python中plt.legend_matplotlib中plt.legend等的使⽤⽅法plt.lengend()
⽤于给图像加图例。
图例是集中于地图⼀⾓或⼀侧的地图上各种符号和颜⾊所代表内容与指标的说明,有助于更好的认识地图。
语法参数如下: matplotlib.pyplot.legend(*args, **kwargs)
keyword
Description
loc
Location code string, or tuple (see below).图例所有figure位置
prop
the font property字体参数
fontsize
the font size (used only if prop is not specified)
markerscale
the relative size of legend markers vs. original
图例标记与原始标记的相对⼤⼩
markerfirst
If True (default), marker is to left of the label.
如果为True,则图例标记位于图例标签的左侧
numpoints
the number of points in the legend for line
为线条图图例条⽬创建的标记点数
scatterpoints
the number of points in the legend for scatter plot
为散点图图例条⽬创建的标记点数
scatteryoffsets
a list of yoffsets for scatter symbols in legend
为散点图图例条⽬创建的标记的垂直偏移量
frameon
If True, draw the legend on a patch (frame).
控制是否应在图例周围绘制框架
matplotlib中subplotfancybox
If True, draw the frame with a round fancybox.
控制是否应在构成图例背景的FancyBboxPatch周围启⽤圆边
shadow
If True, draw a shadow behind legend.
控制是否在图例后⾯画⼀个阴
framealpha
Transparency of the frame.
控制图例框架的 Alpha 透明度
edgecolor
Frame edgecolor.
facecolor
Frame facecolor.
ncol
number of columns 设置图例分为n列展⽰borderpad
the fractional whitespace inside the legend border 图例边框的内边距
labelspacing
the vertical space between the legend entries
图例条⽬之间的垂直间距
handlelength
the length of the legend handles
图例句柄的长度
handleheight
the height of the legend handles
图例句柄的⾼度
handletextpad
the pad between the legend handle and text
图例句柄和⽂本之间的间距
borderaxespad
the pad between the axes and legend border
轴与图例边框之间的距离
columnspacing
the spacing between columns 列间距
title
the legend title
bbox_to_anchor
the bbox that the legend will be anchored.指定图例在轴的位置
bbox_transform
the transform for the bbox. transAxes if None.
常⽤的⼏个参数:
(1)设置图列位置
plt.legend(loc='upper center')
0: ‘best'
1: ‘upper right'
2: ‘upper left'
3: ‘lower left'
4: ‘lower right'
5: ‘right'
6: ‘center left'
7: ‘center right'
8: ‘lower center'
9: ‘upper center'
10: ‘center'
(2)设置图例字体⼤⼩
fontsize : int or float or {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’} (3)设置图例边框及背景
plt.legend(loc='best',frameon=False) #去掉图例边框
plt.legend(loc='best',edgecolor='blue') #设置图例边框颜⾊
plt.legend(loc='best',facecolor='blue') #设置图例背景颜⾊,若⽆边框,参数⽆效
对于边框还可以采⽤⾯向对象⽅式:
legend = plt.legend(["First", "Second"])
frame = _frame()
frame.set_facecolor('blue')
(4)设置图例标题
legend = plt.legend(["CH", "US"], title='China VS Us')
(5)设置图例名字及对应关系
legend = plt.legend([p1, p2], ["CH", "US"])
from matplotlib importpyplot as pltimportnumpy as np
train_x= np.linspace(-1, 1, 100)
train_y_1= 2*train_x + np.random.rand(*train_x.shape)*0.3train_y_2= train_x**2+np.random.randn(*train_x.shape)*0.3p1= plt.scatter(train_x, train_y_1, c='red', marker='v')
p2= plt.scatter(train_x, train_y_2, c='blue', marker='o')
legend= plt.legend([p1, p2], ["CH", "US"], facecolor='blue')
plt.show()
plt.scatter()
⽤于画散点图。
x,y
X和Y是长度相同的数组
s
size,点的⼤⼩,标量或与数据长度相同的数组
c
color,点的颜⾊,标量或与数据长度相同的数组
marker
MarketStyle,可选,点的形状,默认'o'
cmap
Colormap,可选,默认'None'
norm
Normalize,亮度设置,0-1
vmin,vmax
亮度设置
alpha
透明度,0-1
linewidths
其中散点的形状参数marker如下:
其中颜⾊参数c如下:
scatter(x, y, 点的⼤⼩, 颜⾊,标记),这是最主要的⼏个⽤法,如果括号中不写s= c=则按默认顺序,写了则按规定的来,不考虑顺序
importmatplotlib.pyplot as plt#x,y,⼤⼩,颜⾊
plt.scatter([1,2,3,4],[2,4,6,8],[10,20,30,400],['r', 'b','y','k'])
plt.scatter([1,2,3,4],[9,8,7,6],s=10,c='b', marker='v')
plt.show()
importnumpy as npimportmatplotlib.pyplot as plt#Fixing random state for reproducibility
np.random.seed(19680801)
N= 50x=np.random.rand(N)
y=np.random.rand(N)
colors=np.random.rand(N)
area= (30 * np.random.rand(N))**2 #0 to 15 point radii plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
plt.plot()
⼀个通⽤命令,将(x, y)绘制成线条或散点图。
x, y
数据,x是可选的,默认range(len(y))
fmt
format,格式,形状,例如,'ro'表⽰红圈
data
标有数据的对象,可选
其中,线条的格式还可以使⽤如下线属性:
alpha
float,透明度
fillstyle
{'full', 'left', 'right', 'bottom', 'top', 'none'}
linestyle/ls
{'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
linewidth/lw
float
marker
marker style
markeredgecolor/mec
color
markeredgewidth/mew
float
markerfacecolor/mfc
color
markersize/ms
float
...
...
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论