当前位置:Gxlcms > Python > python如何绘制降水图

python如何绘制降水图

时间:2021-07-01 10:21:17 帮助过:8人阅读

python能快速解决日常工作中的小任务,比如数据展示。python做数据展示,主要用到matplotlib库,使用简单的代码,就可以很方便的绘制折线图、柱状图等。使用Java等,可能还需要配合html来进行展示,十分繁琐。

各种平面图的绘制代码:

  1. '''
  2. File Name: draw
  3. Description: 图形绘制。十分有用,对于工作中实验性的项目,可以快速展示效果。如果使用java,还需要配合前端展示。
  4. '''
  5. import matplotlib.pyplot as plt
  6. import numpy as np # 模块取别名
  7. # 直方图
  8. def draw_hist():
  9. mu = 100
  10. sigma = 20
  11. x = mu + sigma * np.random.randn(20000) # 样本数量
  12. plt.hist(x, bins=100, color='green', normed=True) # bins:显示有几个直方,normed是否对数据进行标准化
  13. plt._show()
  14. # 条形图
  15. def draw_bar():
  16. y = [20, 10, 30, 25, 15] # Y轴数据
  17. index = np.arange(5) # X轴数据,也可以是index = [0,5]
  18. plt.bar(left=index, height=y, color='blue', width=0.5)
  19. plt.show()
  20. # 折线图
  21. def draw_plot():
  22. x = np.linspace(-10, 10, 100) # -10到10,100个点
  23. y = x ** 3 # x的3次幂
  24. plt.plot(x, y, linestyle='--', color='orange', marker='<')
  25. plt.xlabel('X')
  26. plt.ylabel('Y')
  27. plt.show()
  28. # 散点图
  29. def draw_scatter():
  30. x = np.random.randn(1000)
  31. y = x + np.random.randn(1000) * 0.5
  32. plt.scatter(x, y, s=5, marker='<') # s表示面积,marker表示图形
  33. plt.show()
  34. # 饼状图
  35. def draw_pie():
  36. labels = 'A', 'B', 'C', 'D' # 4个模块
  37. fracs = [15, 30, 45, 10] # 每个模块占比例
  38. plt.axes(aspect=1) # 使x、y轴比例相同
  39. explode = [0, 0.5, 0, 0] # 突出某一部分区域
  40. plt.pie(x=fracs, labels=labels, autopct='%.0f%%', explode=explode) # autopct显示百分比
  41. plt.show()
  42. # 带图例
  43. def draw_with_legend():
  44. x = np.arange(1, 11, 1) # x轴坐标,1开始,11结束,步长为1
  45. plt.plot(x, x * 2) # 第一条线,x,y坐标
  46. plt.plot(x, x * 3)
  47. plt.plot(x, x * 4)
  48. plt.legend(['Normal', 'Fast', 'Faster']) # 设置图例,与上面的线对应
  49. plt.grid(True, color='green', linestyle='--', linewidth=1) # 绘制网格
  50. plt.show()
  51. # start
  52. if __name__ == '__main__':
  53. # draw_hist()
  54. # draw_bar()
  55. draw_plot()
  56. # draw_scatter()
  57. # draw_pie()
  58. # draw_with_legend()

3D图的绘制代码:

  1. '''
  2. File Name: draw_3d
  3. Description: 3D绘图
  4. '''
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. from mpl_toolkits.mplot3d import Axes3D
  8. # 3D 绘制
  9. def draw_3D():
  10. fig = plt.figure() # 定义一个窗口
  11. ax = Axes3D(fig) # 绘制3D坐标
  12. # 设置x、y、z的值
  13. x = np.arange(-4, 4, 0.25)
  14. y = np.arange(-4, 4, 0.25)
  15. x, y = np.meshgrid(x, y) # x-y 平面的网格
  16. r = np.sqrt(x ** 2 + y ** 2)
  17. z = np.sin(r) # z值
  18. # 做出一个三维曲面,并将一个 colormap rainbow 填充颜色,之后将三维图像投影到 XY 平面上做一个等高线图
  19. # rstride 和 cstride 分别代表 row 和 column 的跨度。
  20. ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
  21. # 添加 XY 平面的等高线
  22. ax.contourf(x, y, z, zdir='z', offset=-2, cmap=plt.get_cmap('rainbow'))
  23. ax.set_zlim(-2, 2)
  24. plt.show() # 展示
  25. # start
  26. if __name__ == '__main__':
  27. draw_3D()

以上就是python如何绘制降水图的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行