日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python legend位置_关于matplotlib-legend 位置属性 loc 使用说明

發(fā)布時間:2024/1/23 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python legend位置_关于matplotlib-legend 位置属性 loc 使用说明 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在使用matplotlib畫圖時,少不了對性能圖形做出一些說明和補充。一般情況下,loc屬性設(shè)置為'best'就足夠應(yīng)付了

plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 'best')

或直接loc = 0

plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 0)

除'best',另外loc屬性有:

'upper right', 'upper left', 'lower left', 'lower right', 'right', 'center left', 'center right', 'lower center', 'upper center', 'center'

不說太多,上面是全部的快捷使用,滿足一般需求。

demo:

import matplotlib.pyplot as plt

import numpy as np

# 繪制普通圖像

x = np.linspace(-1, 1, 50)

y1 = 2 * x + 1

y2 = x**2

plt.figure()

# 在繪制時設(shè)置lable, 逗號是必須的

l1, = plt.plot(x, y1, label = 'line')

l2, = plt.plot(x, y2, label = 'parabola', color = 'red', linewidth = 1.0, linestyle = '--')

# 設(shè)置坐標軸的取值范圍

plt.xlim((-1, 1))

plt.ylim((0, 2))

# 設(shè)置坐標軸的lable

plt.xlabel('X axis')

plt.ylabel('Y axis')

# 設(shè)置x坐標軸刻度, 原來為0.25, 修改后為0.5

plt.xticks(np.linspace(-1, 1, 5))

# 設(shè)置y坐標軸刻度及標簽, $$是設(shè)置字體

plt.yticks([0, 0.5], ['$minimum$', 'normal'])

# 設(shè)置legend

plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 'best')

plt.show()

運行結(jié)果:

補充知識:設(shè)置圖列(key/legend)的位置和大小 --gnuplot

先看幾個例子:

//不顯示圖例。

unset key

//設(shè)置圖例 顯示在圖形(內(nèi))的頂部居中,并且多個圖例水平顯示。

set key top horizontal center

//設(shè)置圖例 顯示在圖形(外)的頂部居中,并且多個圖例水平顯示。

set key top outside horizontal center

//設(shè)置圖例 顯示的字體并加粗。

set key font "Times,18,Bold"

//調(diào)整圖例行間隔

set key spacing 3

//調(diào)整圖例中線段示例長度

set key samplen 2

set key 的語法規(guī)則

Syntax:

set key {on|off} {default}

{{inside | outside} | {lmargin | rmargin | tmargin | bmargin}

| {at }}

{left | right | center} {top | bottom | center}

{vertical | horizontal} {Left | Right}

{{no}reverse} {{no}invert}

{samplen } {spacing }

{width }

{height }

{{no}autotitle {columnheader}}

{title ""} {{no}enhanced}

{{no}box { {linestyle | ls }

| {linetype | lt }

{linewidth | lw }}}

unset key

show key

Elements within the key are stacked according to vertical or horizontal. In the case of vertical, the key occupies as few columns as possible. That is, elements are aligned in a column until running out of vertical space at which point a new column is started. In the case of horizontal, the key occupies as few rows as possible.

圖例是依據(jù)我們設(shè)置的水平顯示或垂直顯示進行堆疊式地顯示。

對于垂直顯示,pnuplot會占用盡可能少的行來放置我們的圖例,當圖例在一行顯示不下時,它會另啟一行來顯示。

對于水平顯示方式,pnuplot會占用盡可能少的列來放置我們的圖例,當圖例在一列顯示不下時,它會另啟一列來放置。

The vertical spacing between lines is controlled by spacing. The spacing is set equal to the product of the pointsize, the vertical tic size, and vertical_spacing. The program will guarantee that the vertical spacing is no smaller than the character height.

The defaults for set key are on, right, top, vertical, Right, noreverse, noinvert, samplen 4, spacing 1.25, title “”, and nobox.

以上這篇關(guān)于matplotlib-legend 位置屬性 loc 使用說明就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

總結(jié)

以上是生活随笔為你收集整理的python legend位置_关于matplotlib-legend 位置属性 loc 使用说明的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。