-
2021-05-03 14:54:55
用legend(fontsize=)方法是无效的,需要添加plt的属性参数
plt.rcParams.update({'font.size':18})
更多相关内容 -
plt.plot(),plt.scatter(),plt.legend函数的用法介绍
2021-11-24 13:21:56plt.plot()函数 plt.plot(x, y, format_string, **kwargs) 参数 说明 x X轴数据,列表或数组,可选 y Y轴数据,列表或数组 format_string 控制曲线的格式字符串,可选 **kwargs 第二组或更多(x,y,...plt.plot()函数
plt.plot(x, y, format_string, **kwargs)
参数 说明 x
X轴数据,列表或数组,可选 y
Y轴数据,列表或数组 format_string
控制曲线的格式字符串,可选 **kwargs
第二组或更多(x,y,format_string),可画多条曲线 format_string
由颜色字符、风格字符、标记字符组成- 颜色字符
'b'
蓝色'm'
洋红色 magenta'g'
绿色'y'
黄色'r'
红色'k'
黑色'w'
白色'c'
青绿色 cyan'#008000'
RGB某颜色'0.8'
灰度值字符串- 多条曲线不指定颜色时,会自动选择不同颜色
- 风格字符
'‐'
实线'‐‐'
破折线'‐.'
点划线':'
虚线'' ' '
无线条
- 标记字符
'.'
点标记','
像素标记(极小点)'o'
实心圈标记'v'
倒三角标记'^'
上三角标记'>'
右三角标记'<'
左三角标记…等等
**kwargs
: 第二组或更多(x,y,format_string)color
: 控制颜色, color=‘green’
linestyle
: 线条风格, linestyle=‘dashed’
marker
: 标记风格, marker=‘o’
markerfacecolor
: 标记颜色, markerfacecolor=‘blue’
markersize
: 标记尺寸, markersize=20b = np.arange(5) plt.plot(b,b*1.0,'g.-',b,b*1.5,'rx',b,b*2.0, 'b') plt.show()
plt.scatter()函数
plt.scatter()函数用于生成一个scatter散点图。
matplotlib.pyplot.scatter(x, y, s=20, c='b', marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None, **kwargs
参数 解释说明 x,y 表示的是shape大小为(n,)的数组,也就是我们即将绘制散点图的数据点,输入数据。 s 表示的是大小,是一个标量或者是一个shape大小为(n,)的数组,可选,默认20。 c 表示的是色彩或颜色序列,可选,默认蓝色’b’。但是c不应该是一个单一的RGB数字,也不应该是一个RGBA的序列,因为不便区分。c可以是一个RGB或RGBA二维行数组。 marker MarkerStyle,表示的是标记的样式,可选,默认’o’。 cmap Colormap,标量或者是一个colormap的名字,cmap仅仅当c是一个浮点数数组的时候才使用。如果没有申明就是image.cmap,可选,默认None。 norm Normalize,数据亮度在0-1之间,也是只有c是一个浮点数的数组的时候才使用。如果没有申明,就是默认None。 vmin,vmax 标量,当norm存在的时候忽略。用来进行亮度数据的归一化,可选,默认None。 alpha 标量,0-1之间,可选,默认None。 linewidths 标记点的长度,默认None。 例子
import numpy as np import matplotlib.pyplot as plt np.random.seed(0) x=np.random.rand(20) y=np.random.rand(20) area=(50*np.random.rand(20))**2 plt.scatter(x,y,s=area,alpha=0.5) plt.show()
plt.legend()函数
1.设置图例的位置
plt.legend(loc=' ')
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') #设置图例背景颜色,若无边框,参数无效
4.设置图例标题
legend = plt.legend(["BJ", "SH"], title='Beijing VS Shanghai') #或者 plt.plot(["BJ", "SH"],loc='upper left',title='Beijing VS Shanghai')
5.设置图例名字及对应关系
legend = plt.legend([p1, p2], ["BJ", "SH"])
示例
import matplotlib.pyplot as plt import numpy as np x = np.arange(0,10,1) plt.plot(x,x,'r--',x,np.cos(x),'g--',marker='*') plt.xlabel('row') plt.ylabel('cow') plt.legend(["BJ","SH"],loc='upper left',loc='upper left') plt.show()
运行结果
- 颜色字符
-
plt.legend参数
2019-05-14 17:49:07fontsize: 字体大小, frameon: 是否显示图例边框, ncol: 图例的列的数量,一般为1, title: 为图例添加标题 shadow: 为图例边框添加阴影, markerfirst: True表示图例标签在句柄右侧,false反之, ...loc:图例位置,可取(‘best’, ‘upper right’, ‘upper left’, ‘lower left’, ‘lower right’, ‘right’, ‘center left’, ‘center , right’, ‘lower center’, ‘upper center’, ‘center’) ;若是使用了bbox_to_anchor,则这项就无效了
fontsize: 字体大小,
frameon: 是否显示图例边框,
ncol: 图例的列的数量,一般为1,
title: 为图例添加标题
shadow: 为图例边框添加阴影,
markerfirst: True表示图例标签在句柄右侧,false反之,
markerscale: 图例标记为原图标记中的多少倍大小,
numpoints: 表示图例中的句柄上的标记点的个数,一般设为1,
fancybox: 是否将图例框的边角设为圆形
framealpha: 控制图例框的透明度
borderpad: 图例框内边距
labelspacing: 图例中条目之间的距离
handlelength: 图例句柄的长度
bbox_to_anchor: (横向看右,纵向看下),如果要自定义图例位置或者将图例画在坐标外边,用它,比如bbox_to_anchor=(1.4,0.8),这个一般配合着ax.get_position(),set_position([box.x0, box.y0, box.width*0.8 , box.height])使用''' -
matplotlib中plt.legend等的使用方法
2020-12-06 06:52:01plt.lengend()用于给图像加图例。图例是集中于地图一角或一侧的地图上各种符号和颜色所代表内容与指标的说明,有助于更好的认识地图。语法参数如下: matplotlib.pyplot.legend(*args,**kwargs)...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).
控制是否应在图例周围绘制框架
fancybox
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 = legend.get_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
...
...
当线属性与fmt冲突时,线属性优先。
一些默认的写法:
>>> plot(x, y) #plot x and y using default line style and color
>>> plot(x, y, 'bo') #plot x and y using blue circle markers
>>> plot(y) #plot y using x as index array 0..N-1
>>> plot(y, 'r+') #ditto, but with red plusses
importnumpy as npimportmatplotlib.pyplot as plt
X= np.linspace(0, 2*np.pi, 32, endpoint=True)
C,S=np.cos(X), np.sin(X)#plt.plot(X,C)#plt.plot(X,S)
plt.plot(X, C, 'go--')
plt.plot(X,S, color='green', marker='o', linestyle='dashed')#一个是fmt,一个是线属性,但是它们的格式是一样的
plt.show()
plt.figure()
用于创建一个新图。
num
新图的编号,默认递增
figsize
宽度,高度,以英寸为单位
dpi
分辨率,整数
facecolor
背景颜色
edgecolor
边框颜色
frameon
若为False,则没有边框
clear
若为True,如果图的编号已存在则先清除
如果有多个figure,请显示的调用 pyplot.close() 关闭你不需要使用的figure,以便pyplot能正确的清理内存。
importnumpy as npimportmatplotlib.pyplot as plt
X= np.linspace(0, 2*np.pi, 32, endpoint=True)
C,S=np.cos(X), np.sin(X)
plt.figure(num=1)
plt.plot(X,C)#plt.close()
plt.figure(2)
plt.plot(X,S)
plt.figure(3)
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.show()
会连续展现三张图,注意与下面subplot的区别。
对于只有一张图时,也有作用,例如设置尺寸和分辨率等:
#创建一个8x6大小的figure,并设置每英寸80个像素点
plt.figure(figsize=(8, 6), dpi=80)
plt.subplot()
用于在一个Figure对象里画多个子图(Axes)。
其调用格式:subplot(numRows, numCols, plotNum),即(行、列、序号)。
图表的整个绘图区域被分成numRows行和numCols列,plotNum参数指定创建的Axes对象所在的区域(左上角序号为1)
如果行数、列数和序号都是个位数可以简写成一个整数,否则需要用逗号隔开。
importnumpy as npimportmatplotlib.pyplot as plt
X= np.linspace(0, 2*np.pi, 32, endpoint=True)
C,S=np.cos(X), np.sin(X)
plt.subplot(221)
plt.plot(X,C)
plt.subplot(2,2,2) #可以隔开,也可以不隔开
plt.plot(X,S)
plt.subplot(212)
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.show()
参考链接:
-
Python-plt绘制折线图legend
2021-03-08 18:34:53目录 简介 主要参数分析 颜色color 风格linestyle(可自己设置linewidth 图例位置loc(默认即可) ... legend-matplotlib官方手册... plot与legend画图与图例 py.. -
Python:plt.legend或者ax.legend设置图例的参数详解
2019-04-23 18:30:50...plt.legend或者ax.legend设置图例的参数详解 #显示图例 # box = ax.get_position() # ax.set_position([box.x0, box.y0, box.wi... -
如何更改单个图例标签的大小?
2021-06-13 11:42:32如图所示,相同的希腊字母字体大小...图的代码如下:import numpy as npimport mathimport matplotlib.pyplot as pltalpha=np.arange(0,1,0.01)gamma=np.sin(2*np.pi*alpha)x=alphay=np.cos(2*np.pi*x)plt.plot(x,y... -
「转」plt.legend()简明使用教程
2020-03-27 17:51:25原文链接https://blog.csdn.net/helunqu2017/article/details/78641290,感谢作者辛勤付出,仅作笔记...legend语法参数如下: matplotlib.pyplot.legend(*args, **kwargs) \ Keyword Description loc Lo... -
对Python中plt的画图函数详解
2020-12-14 00:59:251、plt.legendplt.legend(loc=0)#显示图例的位置,自适应方式说明:'best' : 0, (only implemented for axes legends)(自适应方式)'upper right' : 1,'upper left' : 2,'lower left' : 3,'lower right' : 4,'right' ... -
plt.legend()的几种用法
2020-08-30 17:00:17plt.legend()的几种用法 ** (1)设置图列位置 ```python plt.legend(loc=' ')  **(2)设置图例字体大小** ```python fontsize : int or float ... -
Matplotlib——图形的完善修饰plt.legend、plt.gca、set_major_formatter
2020-03-26 20:58:33图例的注释plt.legend() plt.legend(loc=’’, title=,fontsize=12, frameon=True, fancybox=True, framealpha=0.2, borderpad=0.3, ncol=1, markerfirst=True, markerscale=1, bbox_to_anchor=,numpoints=1, ... -
python中matplotlib的颜色及线条控制【以及改变legend字体大小】
2017-06-01 10:46:16改变legend字体大小 https://zhidao.baidu.com/question/1754404674146091188.html 去掉plt.legend()改为---->>> plt.legend(loc=0, numpoints=1) leg = plt.gca().get_legend() l... -
Python plt.legend绘图图例显示不全只显示一个字符
2021-08-29 09:51:48上原码: ax.legend(labels = '...原因是因为将图例放到legend里了,应该放到plt.plot里,改后代码: plt.plot(timeData,intensityData,c = 'blue',linewidth = 1,label='band') ax.legend(loc='best') plt.plot(timeD -
Py之matplotlib.pyplot:matplotlib.pyplot的plt.legend函数的简介、使用方法之详细攻略
2021-01-04 23:41:54Py之matplotlib.pyplot:matplotlib.pyplot的plt.legend函数的简介、使用方法之详细攻略 目录 matplotlib.pyplot的plt.legend函数的简介 1、参数解释 2、源代码 matplotlib.pyplot的plt.legend函数的... -
关于python 的legend图例,参数使用说明
2020-12-05 18:09:12y,'.')画出的散点图中图例是两个点(因为plot默认画的是线,需要两个端点来表示线,所以是两个点),matplotlib.pyplot.scatter(x,y,'.')画出的散点图中图例是三个点(这个我理解不了为什么,scatter散点的大小可以自己... -
matplotlib里面plt.plot()画图figsize、legend等不生效的问题
2022-04-15 19:07:00好久不写py,做个行情线,但图太小,用plt.figure()设置画布大小之后,怎么样都不生效,最后发现是因为放错了顺序,调用figure方法一定要在填充数据plot()之前。 解决完后想加个图例(legend方法)也加不上去,最后... -
plt从入门到入土
2021-12-26 22:38:19import matplotlib.pyplot as plt x = np.linspace(0.5, 3.5, 100) y = np.sin(x) y1 = np.random.uniform(-10,10, 100) t = np.linspace(-10, 10, 100) sig = 1 / (1 + np.exp(-t)) ax=plt.gca() ax.spines['... -
matplotlib可视化之饼图plt.pie()与plt.legend()中bbox_to_anchor参数的理解
2021-08-21 12:10:09调用方法:plt.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, ... -
在matplotlib中移动图例框并调整其大小
2021-07-19 16:25:38np.linspace(0,1,100) y = x**2 import matplotlib.pyplot as plt plt.plot(x, y, label = '{\\footnotesize \$y = x^2\$}') plt.legend(loc = 'best') plt.show() The problem is, as you can see, that the ... -
python - matplotlib.legend()函数用法解析
2019-09-25 17:40:211.图例legend基础语法及用法 legend语法参数如下: matplotlib.pyplot.legend(*args,**kwargs) Keyword Description loc Location code string, or tuple (see below).图例所有figure位置 prop the font ... -
matplotlib.pyplot绘制legend、特殊符号、设置坐标轴Ticks
2020-12-16 06:40:57## set legend ax1.legend(loc='upper left',fontsize=8); 设置坐标轴的Ticks。下面分别展示两种不同方法。设置X轴ticks的方法可以指定major ticks,minor ticks的间隔,在科学绘图中应该是非常有用了,还可以设置... -
matlplotlib绘图 之 函数legend()
2019-08-16 19:55:361.【推荐使用】在plot函数中增加label参数,然后在后面加上plt.legend() plt.plot(x, x*3.0, label='Fast') plt.plot(x, x/3.0, label='Slow') plt.legend() 2.legend方法中传入字符串列表 plt.plot(x,np.sin... -
matplotlib如何改变legend的字号
2017-09-04 16:18:25改变legend的字号大小plt.legend(loc = 0, prop = {'size':8}) -
Python matplotlib库绘图时设置标题 (label)、坐标轴 (axis) 和标注 (legend)的字体及大小
2021-10-30 10:53:13plt.xlabel('False positive rate', fontdict={"family": "Times New Roman", "size": 25}) plt.ylabel('True positive rate', fontdict={"family": "Times New Roman", "size": 25}) 2 坐标轴 plt.xticks(fontname... -
Matplotlib配置图例legend()设置透明和并排显示
2022-03-26 16:09:451.多排显示 x=np.linspace(start=-np.pi,stop=np.pi,num=300) plt.style.use('classic') Fig,Axes=plt.subplots(1) Axes.plot(x,np.sin(x),'-b',label='Sine') ...Axes.legend(loc='lower center',frameon=False -
python画图的图例legend设置。
2018-07-07 19:29:354,设置图例字体大小,fontsize参数 : int or float or {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’},只有这7种调节值。 也可以利用prop参数:先定义 font1 =... -
python Matplotlib画图之调整字体大小的示例
2021-01-21 17:44:00一张字体调整好的示例图: 字体大小就是 fontsize 参数 import matplotlib.pyplot as plt # 代码中的“...”代表省略的其他参数 ax = plt.subplot(111) ...ax.legend(..., fontsize=20) 实战: import matplotlib. -
matplotlib savefig 保存图片大小的实例
2020-12-23 15:26:45plt.legend(bbox_to_anchor=(1.01, 1), loc=2, borderaxespad=0., handleheight=1.675) 这个语句可以解决图例遮挡线条的问题,同时,也引入了另外的问题:会使savefig保存图片时,不能将图片完整保存,会使图例保存...