ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

数据分析-画图工具 matplotlib

2022-07-05 01:31:16  阅读:278  来源: 互联网

标签:数据分析 set axes 画图 matplotlib annot plt ind


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:

最大化图画 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:

  1. 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.

  1. Change GUI toolkit of the ipython kernel to widget
%matplotlib widget
# or %matplotlib ipympl, but recommended the above
# plotting
  1. 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

标签:数据分析,set,axes,画图,matplotlib,annot,plt,ind
来源: https://www.cnblogs.com/xmaples/p/16444997.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有