数据可视化 | Matplotlib

举报
DrugAI 发表于 2021/07/15 04:26:39 2021/07/15
【摘要】 Matplotlib 是一个 Python 的 2D绘图库,通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,条形图,错误图,散点图等。 http://matplotlib.org 用于创建出版质量图表的绘图工具库 目的是为Python构建一个Matlab式的绘图接口 import matplotlib.pyplot as p...

Matplotlib 是一个 Python 的 2D绘图库,通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,条形图,错误图,散点图等。

http://matplotlib.org

  • 用于创建出版质量图表的绘图工具库

  • 目的是为Python构建一个Matlab式的绘图接口

  • import matplotlib.pyplot as plt
       

     

  • pyploy模块包含了常用的matplotlib API函数

 

figure

  • Matplotlib的图像均位于figure对象中

  • 创建figure:fig = plt.figure()

 


  
  1. # 引入matplotlib包
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. %matplotlib inline #在jupyter notebook 里需要使用这一句命令
  5. # 创建figure对象
  6. fig = plt.figure()

运行结果:

<matplotlib.figure.Figure at 0x11a2dd7b8>

 

subplot

fig.add_subplot(a, b, c)

  • a,b 表示将fig分割成 a*b 的区域

  • c 表示当前选中要操作的区域,

  • 注意:从1开始编号(不是从0开始)

  • plot 绘图的区域是最后一次指定subplot的位置 (jupyter notebook里不能正确显示)

 


  
  1. # 指定切分区域的位置
  2. ax1 = fig.add_subplot(2,2,1)
  3. ax2 = fig.add_subplot(2,2,2)
  4. ax3 = fig.add_subplot(2,2,3)
  5. ax4 = fig.add_subplot(2,2,4)
  6. # 在subplot上作图
  7. random_arr = np.random.randn(100)
  8. #print random_arr
  9. # 默认是在最后一次使用subplot的位置上作图,但是在jupyter notebook 里可能显示有误
  10. plt.plot(random_arr)
  11. # 可以指定在某个或多个subplot位置上作图
  12. # ax1 = fig.plot(random_arr)
  13. # ax2 = fig.plot(random_arr)
  14. # ax3 = fig.plot(random_arr)
  15. # 显示绘图结果
  16. plt.show()

运行结果:

 

直方图:hist

 


  
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. plt.hist(np.random.randn(100), bins=10, color='b', alpha=0.3)
  4. plt.show()

运行结果:

 

散点图:scatter

 


  
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # 绘制散点图
  4. x = np.arange(50)
  5. y = x + 5 * np.random.rand(50)
  6. plt.scatter(x, y)
  7. plt.show()

运行结果: 

柱状图:bar

 


  
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # 柱状图
  4. x = np.arange(5)
  5. y1, y2 = np.random.randint(1, 25, size=(2, 5))
  6. width = 0.25
  7. ax = plt.subplot(1,1,1)
  8. ax.bar(x, y1, width, color='r')
  9. ax.bar(x+width, y2, width, color='g')
  10. ax.set_xticks(x+width)
  11. ax.set_xticklabels(['a', 'b', 'c', 'd', 'e'])
  12. plt.show()

运行结果:

 

矩阵绘图:plt.imshow()

  • 混淆矩阵,三个维度的关系

 


  
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # 矩阵绘图
  4. m = np.random.rand(10,10)
  5. print(m)
  6. plt.imshow(m, interpolation='nearest', cmap=plt.cm.ocean)
  7. plt.colorbar()
  8. plt.show()

运行结果:


  
  1. [[ 0.92859942 0.84162134 0.37814667 0.46401549 0.93935737 0.0344159
  2. 0.56358375 0.75977745 0.87983192 0.22818774]
  3. [ 0.88216959 0.43369207 0.1303902 0.98446182 0.59474031 0.04414217
  4. 0.86534444 0.34919228 0.53950028 0.89165269]
  5. [ 0.52919761 0.87408715 0.097871 0.78348534 0.09354791 0.3186
  6. 0.25978432 0.48340623 0.1107699 0.14065592]
  7. [ 0.90834516 0.42377475 0.73042695 0.51596826 0.14154431 0.22165693
  8. 0.64705882 0.78062873 0.55036304 0.40874584]
  9. [ 0.98853697 0.46762114 0.69973423 0.7910757 0.63700306 0.68793919
  10. 0.28685306 0.3473426 0.17011744 0.18812329]
  11. [ 0.73688943 0.58004874 0.03146167 0.08875797 0.32930191 0.87314734
  12. 0.50757536 0.8667078 0.8423364 0.99079049]
  13. [ 0.37660356 0.63667774 0.78111565 0.25598593 0.38437628 0.95771051
  14. 0.01922366 0.37020219 0.51020305 0.05365718]
  15. [ 0.87588452 0.56494761 0.67320078 0.46870376 0.66139913 0.55072149
  16. 0.51328222 0.64817353 0.198525 0.18105368]
  17. [ 0.86038137 0.55914088 0.55240021 0.15260395 0.4681218 0.28863395
  18. 0.6614597 0.69015592 0.46583629 0.15086562]
  19. [ 0.01373772 0.30514083 0.69804049 0.5014782 0.56855904 0.14889117
  20. 0.87596848 0.29757133 0.76062891 0.03678431]]

 

 

plt.subplots()

  • 同时返回新创建的figuresubplot对象数组

  • 生成2行2列subplot:fig, subplot_arr = plt.subplots(2,2)

  • 在jupyter里可以正常显示,推荐使用这种方式创建多个图表

 


  
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. fig, subplot_arr = plt.subplots(2,2)
  4. # bins 为显示个数,一般小于等于数值个数
  5. subplot_arr[1,0].hist(np.random.randn(100), bins=10, color='b', alpha=0.3)
  6. plt.show()

运行结果: 

颜色、标记、线型

  • ax.plot(x, y, ‘r--’)

等价于ax.plot(x, y, linestyle=‘--’, color=‘r’)

示例代码:


  
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. fig, axes = plt.subplots(2)
  4. axes[0].plot(np.random.randint(0, 100, 50), 'ro--')
  5. # 等价
  6. axes[1].plot(np.random.randint(0, 100, 50), color='r', linestyle='dashed', marker='o')

运行结果:

[<matplotlib.lines.Line2D at 0x11a901e80>]

 

  • 常用的颜色、标记、线型

 

 

 

 

刻度、标签、图例

  • 设置刻度范围

    plt.xlim(), plt.ylim()

    ax.set_xlim(), ax.set_ylim()

  • 设置显示的刻度

    plt.xticks(), plt.yticks()

    ax.set_xticks(), ax.set_yticks()

  • 设置刻度标签

    ax.set_xticklabels(), ax.set_yticklabels()

  • 设置坐标轴标签

    ax.set_xlabel(), ax.set_ylabel()

  • 设置标题

    ax.set_title()

  • 图例

    ax.plot(label=‘legend’)

    ax.legend(), plt.legend()

    loc=‘best’:自动选择放置图例最佳位置

 


  
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. fig, ax = plt.subplots(1)
  4. ax.plot(np.random.randn(1000).cumsum(), label='line0')
  5. # 设置刻度
  6. #plt.xlim([0,500])
  7. ax.set_xlim([0, 800])
  8. # 设置显示的刻度
  9. #plt.xticks([0,500])
  10. ax.set_xticks(range(0,500,100))
  11. # 设置刻度标签
  12. ax.set_yticklabels(['Jan', 'Feb', 'Mar'])
  13. # 设置坐标轴标签
  14. ax.set_xlabel('Number')
  15. ax.set_ylabel('Month')
  16. # 设置标题
  17. ax.set_title('Example')
  18. # 图例
  19. ax.plot(np.random.randn(1000).cumsum(), label='line1')
  20. ax.plot(np.random.randn(1000).cumsum(), label='line2')
  21. ax.legend()
  22. ax.legend(loc='best')
  23. #plt.legend()

运行结果: <matplotlib.legend.Legend at 0x11a4061d0> 


参考资料

https://matplotlib.org/

 

文章来源: drugai.blog.csdn.net,作者:DrugAI,版权归原作者所有,如需转载,请联系作者。

原文链接:drugai.blog.csdn.net/article/details/104287260

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。