数据分析-画图工具 matplotlib
2022/7/5 6:20:14
本文主要是介绍数据分析-画图工具 matplotlib,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
matplotlib —— 数据图绘制工具
matplotlib 是绘制统计图的python工具。
示例绘图 https://matplotlib.org/tutorials/introductory/sample_plots.html
设置 Settings
# 文字不清晰?清晰度、分辨率不够?用以下设置提高画质 plt.rcParams['figure.dpi'] = 200 plt.rcParams['savefig.dpi'] = 200
多图 subplot
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
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)的。
图例 Legend
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()
数据点注解文本 Annotation text for data points
axes = plt.plot(...) axes.text(x, y, s)
鼠标悬停显字 show annotation text on hover for scatter plot
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:
- https://stackoverflow.com/questions/7908636/possible-to-make-labels-appear-when-hovering-over-a-point-in-matplotlib
最大化图画 Shown in Maximized window
# 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')
Notebook交互式图 Plotting Interactive Figure inside jupyter notebook
IPython中默认使用静态图片(即%matplotlib inline),而不是交互式图片,可通过安装相应插件后用指令%matplotlib widget开启交互式图支持,在一个已运行的kernel中需要刷新网页并且重新运行所有单元格。
Steps to enable supporting of interactive figure:
- Install ipympl from the terminal (or ipython notebook), https://matplotlib.org/ipympl/
pip install ipympl
The extension will be automatically installed into jupyter on version 3.0+ of Jupyter-lab.
- Change GUI toolkit of the ipython kernel to
widget
%matplotlib widget # or %matplotlib ipympl, but recommended the above # plotting
- Refresh the web browser tab, and restart the kernel and run all the cells for already open notebook file.
当处于%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)
不会阻塞。
安装 Installing
pip install matplotlib
FAQ
字体问题
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,且不支持窗口最大化的相关方法。
IPython中交互式画图失败
- 是否用指令开启交互式画图%matplotlib widget?
- 在已启动kernel中重新配置后是否刷新网页并重新启动kernel及运行单元格?
- 是否安装ipympl插件并在jupyter lab中启用?
Apache Echarts
https://echarts.apache.org/zh/index.html
基于JavaScript的画图API工具,由百度贡献的开源。
pyecharts
Apache Echarts的Python版本,https://pyecharts.org 。
这篇关于数据分析-画图工具 matplotlib的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)