matplotlib 是绘制统计图的python工具。
示例绘图 https://matplotlib.org/tutorials/introductory/sample_plots.html
# 文字不清晰?清晰度、分辨率不够?用以下设置提高画质 plt.rcParams['figure.dpi'] = 200 plt.rcParams['savefig.dpi'] = 200
subplot(nrows, ncols, index, **kwargs) subplot(pos, **kwargs) subplot(ax) # subplot中图的编号参数(第3个参数)是以1为起始索引算的 ax=plt.subplot(2,2, 1) # 2x2布局,激活(并返回)第1个图 plt.gca() == ax plt.subplot(2,2, 3) # 2x2布局,激活第3个图,即第2排第1列那个 plt.subplot(2,2, [1,3]) # 类似2x2布局,但1,3位置作为1个图的占位(类似表格跨行) plt.subplot(2,3, 1) #在2行3列的多图中激活第1个子图 plt.scatter(x,y) #在当前激活的(子)图中作图 plt.subplot(2,3, 2) #激活第2个子图 plt.scatter(x,y) #再作图 ...
scatter()
参数:
x, y
横纵轴数据,类似数组的数据系列(向量)。s
标记的大小。c
标记的颜色。一种颜色,或个数与数据点个数一样的颜色序列。alpha
标记的不透明度,值范围0.0~1.0,该参数只能是标量,对所有数据点设置,对每个数据点设置需要通过颜色参数实现.label
图例名称。import matplotlib.pyplot as plt plt.scatter([0.0, 1.0], [0.0, 1.0]) plt.scatter(x,y, c=color_array, s=size_array, alpha=, label='图例名') plt.legend() # 开启图例显示 plt.show()
颜色的不透明度通过alpha
参数指定时,该参数只能对所有点全部设置为相同值,如果要对每个点设置不同的不透明度,则需利用颜色参数c
提供与数据点个数相同的颜色向量来指定,一个颜色是四通道(RGBA)的。
plt.plot(x,y, label='数据系列名称') plt.scatter(x,y, label='') plt.legend() #开启图例显示
axes.set_xlabel() axes.set_ylabel() axes.get_xlabel(), axes.get_ylabel() axes.set_xlim(xmin=, xmax=), axes.set_xlim(left=, right=) axes.set_ylim(ymin=, ymax=), axes.set_ylim(bottom=, top=) # set xlabel by plt plt.xlabel(''), plt.ylabel('') # you can NOT get label by plt.xlabel()
axes = plt.plot(...) axes.text(x, y, s)
fig,ax = plt.subplots() sc = plt.scatter(x,y,c=c, s=100, cmap=cmap, norm=norm) annot = ax.annotate("", xy=(0,0), xytext=(20,20),textcoords="offset points", bbox=dict(boxstyle="round", fc="w"), arrowprops=dict(arrowstyle="->")) annot.set_visible(False) def update_annot(ind): pos = sc.get_offsets()[ind["ind"][0]] annot.xy = pos text = "{}, {}".format(" ".join(list(map(str,ind["ind"]))), " ".join([names[n] for n in ind["ind"]])) annot.set_text(text) annot.get_bbox_patch().set_facecolor(cmap(norm(c[ind["ind"][0]]))) annot.get_bbox_patch().set_alpha(0.4) def hover(event): vis = annot.get_visible() if event.inaxes == ax: cont, ind = sc.contains(event) if cont: update_annot(ind) annot.set_visible(True) fig.canvas.draw_idle() else: if vis: annot.set_visible(False) fig.canvas.draw_idle() fig.canvas.mpl_connect("motion_notify_event", hover) plt.show()
References:
# call before plt.show() # or call before .plot() if in plot inline mode # for backend Qt plt.get_current_fig_manager().window.showMaximized() # for backend wxAgg plt.get_current_fig_manager().frame.Maximize(True) # for backend TkAgg on Windows plt.get_current_fig_manager().window.state('zoomed')
IPython中默认使用静态图片(即%matplotlib inline),而不是交互式图片,可通过安装相应插件后用指令%matplotlib widget开启交互式图支持,在一个已运行的kernel中需要刷新网页并且重新运行所有单元格。
Steps to enable supporting of interactive figure:
pip install ipympl
The extension will be automatically installed into jupyter on version 3.0+ of Jupyter-lab.
widget
%matplotlib widget # or %matplotlib ipympl, but recommended the above # plotting
当处于%matplotlib widget
而非%matplotlib inline
时,每个cell中的plt.plot()会用已有的axes和figure,即使该figure&axes是在上一个cell中被创建,若希望本cell中的plt.plot在新的figure&axes上画图,则可先创建新图plt.subplots()
,或在画图代码前使用%matplotlib
。
例:
# cell 1 %matplotlib widget plt.plot() # here is a figure # cell 2 plt.plot() # 这将在上个cell画出的图中继续画 # cell 3 %matplotlib plt.plot() #新图
在IPython环境中可使用一个magic function来使得在调用时即画图(不用显式调用.show()
)
%matplotlib inline
.show()
会阻塞线程直到关闭图,.show(block=False)
不会阻塞。
pip install matplotlib
RuntimeWarning: Glyph 8722 missing from current font
RuntimeWarning: Glyph 37327 missing from current font
中文显示方框?负号显示方框?找不到字体?
解决办法:
plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False # or matplotlib.rcParams[...]=...
AttributeError: 'FigureManagerInterAgg' object has no attribute 'window'
不同类型的backend的属性是不同的,有的backend并没有window属性,如在QtAgg中有window且有.window.showMaximized(),而IPython kernel默认使用的backend不一定是QtAgg,且不支持窗口最大化的相关方法。
https://echarts.apache.org/zh/index.html
基于JavaScript的画图API工具,由百度贡献的开源。
Apache Echarts的Python版本,https://pyecharts.org 。