matplotlib
from matplotlib import pyplot as plt
1.plt.plot()
matplotlib.pyplot.``plot`(**args*, *scalex=True*, *scaley=True*, *data=None*, ***kwargs*)
#单条线:
plot([x], y, [fmt], data=None, **kwargs)
#多条线一起画
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
可选参数[fmt] 是一个字符串来定义图的基本属性如:颜色(color),点型(marker),线型(linestyle)
参数:
-
color:
image.png -
marker:
image.png -
linestyle:
image.png
2.plt.xlabel() plt.ylabel()
给x轴,y轴加label
2.1 plt.xlim() plt.ylim()
设置坐标轴的取值范围
2.2 plt.xticks() plt.yticks()
设置坐标轴刻度
3.plt.title()
给图片加label
4.plt.legend() 说明
绘制多线图,一定要加上说明label
plt.legend()里面不需要加参数
5.plt.text()
arbitrary text
text(x,y,string,fontsize=15,verticalalignment="top",horizontalalignment="right")
x,y:表示坐标值上的值
string:表示说明文字
fontsize:表示字体大小
verticalalignment:垂直对齐方式 ,参数:[ ‘center’ | ‘top’ | ‘bottom’ | ‘baseline’ ]
horizontalalignment:水平对齐方式 ,参数:[ ‘center’ | ‘right’ | ‘left’ ]
xycoords选择指定的坐标轴系统:
例子
#Create plot
plt.plot(six_months.month, six_months.hours_worked)
#Add annotation "Missing June data" at (2.5, 80)
plt.text(2.5,80,"Missing June data")
#Display graph
plt.show()
6.plt.style.use()
7. plt.bar()
matplotlib.pyplot.bar(left, height, alpha=1, width=0.8, color=, edgecolor=, label=, lw=3)
Make a bar plot,绘制柱状图。
参数
- left:x轴的位置序列,一般采用arange函数产生一个序列;
- height:y轴的数值序列,也就是柱形图的高度,一般就是我们需要展示的数据;
- alpha:透明度
- width:为柱形图的宽度,一般这是为0.8即可;
- color或facecolor:柱形图填充的颜色;
- edgecolor:图形边缘颜色
- label:解释每个图像代表的含义
- linewidth or linewidths or lw:边缘or线的宽度
9.xerr:标量或者数组,可选参数。如果不是None,将把生成的errorbars用在条形图上,默认为None。
10.yerr:标量或者数组,可选参数。如果不是None,将把生成的errorbars用在条形图上,默认为None。
7.1.2 plt.barh()
水平方向的柱状图
横着的柱状图
8.plt.scatter()
散点图
参数
1.color:颜色
2.alpha:透明度
3.marker:标注图形
9.叠层柱状图
注意记得加上plt.legend()
10. plt.hist()
直方图
参数:
1.bins
2.range:
3.normed:正则化 (0,1)之间
在直方图比较两个数据,需要做正则化?
11. plt.clf()
清除整个当前数字。与所有的轴,但离开窗口打开,这样它就可以再用在其他的 plots上了。
reference:
https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.plot.html
https://blog.csdn.net/helunqu2017/article/details/78659490
https://blog.csdn.net/liangzuojiayi/article/details/78187704
https://blog.csdn.net/Quincuntial/article/details/70946894
截图来自datacamp课件