当前位置:   article > 正文

python利用matplotlib画图_python画图matplotlib

python画图matplotlib

一、折线图

  1. import matplotlib.pyplot as plt
  2. y1=[10,13,5,40,30,60,70,12,55,25]
  3. x1=range(0,10)
  4. x2=range(0,10)
  5. y2=[5,8,0,30,20,40,50,10,40,15]
  6. plt.plot(x1,y1,label='Frist line',linewidth=3,color='r',marker='o',
  7. markerfacecolor='blue',markersize=12)
  8. plt.plot(x2,y2,label='second line')
  9. plt.xlabel('Plot Number')
  10. plt.ylabel('Important var')
  11. plt.title('Interesting Graph\nCheck it out')
  12. plt.legend()
  13. plt.show()

效果展示:

二、条形图

  1. import matplotlib.pyplot as plt
  2. y1=[10,13,5,40,30,60,70,12,55,25]
  3. x1=range(0,20,2)
  4. x2=range(1,21,2)
  5. y2=[5,8,0,30,20,40,50,10,40,15]
  6. plt.bar(x1,y1,label='Frist line')
  7. #plt.bar(x2,y2,label='second line',color='r')
  8. plt.xlabel('Plot Number')
  9. plt.ylabel('Important var')
  10. plt.title('Interesting Graph\nCheck it out')
  11. plt.legend()
  12. plt.show()

 效果展示:

三、直方图

  1. import matplotlib.pyplot as plt
  2. population_ages = [22,55,62,45,21,22,34,42,42,4,99,102,
  3. 110,120,121,122,130,111,115,112,80,75,
  4. 65,54,44,43,42,48]
  5. x=range(0,130,10)
  6. plt.hist(population_ages,x,rwidth=0.8,color='r',histtype='stepfilled')
  7. plt.xlabel('Plot Number')
  8. plt.ylabel('Important var')
  9. plt.title('Interesting Graph\nCheck it out')
  10. plt.legend()
  11. plt.show()

 效果展示:

四、散点图

  1. import matplotlib.pyplot as plt
  2. population_ages = [22,55,62,45,21,22,34,42,42,4,99,102,
  3. 110,120,121,122,130,111,115,112,80,75,
  4. 65,54,44,43,42,48]
  5. x=range(0,len(population_ages))
  6. plt.scatter(x,population_ages,label='frist label',s=20)
  7. help(plt.scatter)
  8. plt.xlabel('x')
  9. plt.ylabel('y')
  10. plt.title('Interesting Graph\nCheck it out')
  11. plt.legend()
  12. plt.show()

效果展示:

五、饼图

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. y = np.array([35, 25, 25, 15])
  4. plt.pie(y,
  5. labels=['A','B','C','D'], # 设置饼图标签
  6. colors=["#d5695d", "#5d8ca8", "#65a479", "#a564c9"], # 设置饼图颜色
  7. explode=(0, 0.2, 0, 0), # 第二部分突出显示,值越大,距离中心越远
  8. autopct='%.2f%%', # 格式化输出百分比
  9. )
  10. plt.title("RUNOOB Pie Test")
  11. plt.show()

效果展示:

 

六、网线图

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. x = np.array([1, 2, 3, 4])
  4. y = np.array([1, 4, 9, 16])
  5. plt.title("RUNOOB grid() Test")
  6. plt.xlabel("x - label")
  7. plt.ylabel("y - label")
  8. plt.plot(x, y)
  9. plt.grid(color = 'r', linestyle = '--', linewidth = 0.5)
  10. plt.show()

  效果展示:

 七、柱状图

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. x = np.array(["Runoob-1", "Runoob-2", "Runoob-3", "C-RUNOOB"])
  4. y = np.array([12, 22, 6, 18])
  5. plt.bar(x,y)
  6. plt.show()

效果展示:

 

八、绘制多图

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. #plot 1:
  4. x = np.array([0, 6])
  5. y = np.array([0, 100])
  6. plt.subplot(2, 2, 1)
  7. plt.plot(x,y)
  8. plt.title("plot 1")
  9. #plot 2:
  10. x = np.array([1, 2, 3, 4])
  11. y = np.array([1, 4, 9, 16])
  12. plt.subplot(2, 2, 2)
  13. plt.plot(x,y)
  14. plt.title("plot 2")
  15. #plot 3:
  16. x = np.array([1, 2, 3, 4])
  17. y = np.array([3, 5, 7, 9])
  18. plt.subplot(2, 2, 3)
  19. plt.plot(x,y)
  20. plt.title("plot 3")
  21. #plot 4:
  22. x = np.array([1, 2, 3, 4])
  23. y = np.array([4, 5, 6, 7])
  24. plt.subplot(2, 2, 4)
  25. plt.plot(x,y)
  26. plt.title("plot 4")
  27. plt.suptitle("RUNOOB subplot Test")
  28. plt.show()

 效果展示:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/115652
推荐阅读
相关标签
  

闽ICP备14008679号