Python matplotlib
需要安装matplotlib、numpy等模块
基础语法
| import matplotlib.pyplot as plt |
| |
| |
| plt.title('AAPL stock price change') |
| |
| |
| plt.legend() |
| |
| |
| plt.xlabel('time') |
| plt.ylabel('stock price') |
| |
| |
| plt.xlim(datetime(2008,1,1), datetime(2010,12,31)) |
| plt.ylim(0,300) |
| |
| plt.axis([datetime(2008,1,1), datetime(2010,12,31), 0, 300]) |
| |
| |
| plt.xticks() |
| plt.yticks() |
| |
| |
| plt.figure(figsize=[6,6]) |
| |
| |
| plt.annotate() |
| |
| |
| plt.text() |
| |
| AxesSubplot.text() |
基本用法
| import matplotlib.pyplot as plt |
| import numpy as np |
| |
| x = np.linspace(-1, 1, 50) |
| y = 2*x + 1 |
| plt.plot(x, y) |
| plt.show() |

Figure
| import matplotlib.pyplot as plt |
| import numpy as np |
| |
| x = np.linspace(-3, 3, 50) |
| y1 = 2*x + 1 |
| y2 = x ** 2 |
| |
| |
| plt.figure() |
| plt.plot(x, y1) |
| |
| plt.figure() |
| plt.plot(x, y2) |
| |
| plt.figure(num=3, figsize=(8, 5)) |
| plt.plot(x, y2) |
| plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--') |
| |
| |
| plt.show() |
| |



散点图
| import matplotlib.pyplot as plt |
| import numpy as np |
| |
| n = 1024 |
| x = np.random.normal(0, 1, n) |
| y = np.random.normal(0, 1, n) |
| |
| color = np.arctan2(y, x) |
| |
| |
| plt.scatter(x, y, s=75, c=color, alpha=0.5) |
| |
| |
| plt.xlim((-1.5, 1.5)) |
| plt.ylim((-1.5, 1.5)) |
| plt.show() |

柱状图
| import matplotlib.pyplot as plt |
| import numpy as np |
| |
| n = 12 |
| x = np.arange(n) |
| y1 = (1 - x/ float(n)) * np.random.uniform(0.5, 1.0, n) |
| y2 = (1 - x/ float(n)) * np.random.uniform(0.5, 1.0, n) |
| |
| |
| plt.bar(x, +y1, facecolor='#9999ff', edgecolor='white') |
| plt.bar(x, -y2, facecolor='#ff9999', edgecolor='white') |
| |
| |
| |
| for i, j in zip(x, y1): |
| |
| plt.text(i, j + 0.05, '%.2f' % j, ha='center', va='bottom') |
| |
| for i, j in zip(x, y2): |
| |
| plt.text(i, -j - 0.05, '%.2f' % -j, ha='center', va='top') |
| |
| |
| |
| plt.xlim((-.5, n)) |
| plt.ylim((-1.25, 1.25)) |
| plt.show() |

多图合一
subplot
| import matplotlib.pyplot as plt |
| |
| plt.figure() |
| |
| |
| plt.subplot(2, 2, 1) |
| plt.plot([0, 1], [0, 1]) |
| |
| plt.subplot(2, 2, 2) |
| plt.plot([0, 1], [0, 2]) |
| |
| plt.subplot(2, 2, 3) |
| plt.plot([0, 1], [0, 3]) |
| |
| plt.subplot(2, 2, 4) |
| plt.plot([0, 1], [0, 4]) |
| |
| plt.show() |

| import matplotlib.pyplot as plt |
| |
| plt.figure() |
| |
| |
| |
| plt.subplot(2, 1, 1) |
| plt.plot([0, 1], [0, 1]) |
| |
| |
| plt.subplot(2, 3, 4) |
| plt.plot([0, 1], [0, 2]) |
| |
| plt.subplot(2, 3, 5) |
| plt.plot([0, 1], [0, 3]) |
| |
| plt.subplot(2, 3, 6) |
| plt.plot([0, 1], [0, 4]) |
| |
| plt.show() |

分格显示
| import matplotlib.pyplot as plt |
| import matplotlib.gridspec as gridspec |
| |
| |
| |
| plt.figure() |
| |
| |
| |
| ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3) |
| ax1.plot([1, 2], [1, 2]) |
| |
| ax1.set_title('ax1_title') |
| |
| ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2) |
| ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2) |
| |
| ax4 = plt.subplot2grid((3, 3), (2, 0)) |
| ax4.scatter([1, 2], [2, 2]) |
| ax4.set_xlabel('ax4_x') |
| ax4.set_ylabel('ax4_y') |
| |
| ax5 = plt.subplot2grid((3, 3), (2, 1)) |
| |
| plt.tight_layout() |
| plt.show() |
| |
| |
| |
| plt.figure() |
| |
| gs = gridspec.GridSpec(3, 3) |
| |
| |
| ax6 = plt.subplot(gs[0, :]) |
| |
| ax7 = plt.subplot(gs[1, :2]) |
| |
| ax8 = plt.subplot(gs[1:, 2]) |
| |
| ax9 = plt.subplot(gs[-1, 0]) |
| |
| ax10 = plt.subplot(gs[-1, -2]) |
| |
| plt.tight_layout() |
| plt.show() |
| |
| |
| |
| |
| |
| f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True) |
| ax11.scatter([1,2], [1,2]) |
| |
| plt.tight_layout() |
| plt.show() |



图中图
| import matplotlib.pyplot as plt |
| |
| fig = plt.figure() |
| x = [1, 2, 3, 4, 5, 6, 7] |
| y = [1, 3, 4, 2, 5, 8, 6] |
| |
| |
| left, bottom, width, height = 0.1, 0.1, 0.8, 0.8 |
| |
| ax1 = fig.add_axes([left, bottom, width, height]) |
| ax1.plot(x, y, 'r') |
| ax1.set_xlabel('x') |
| ax1.set_ylabel('y') |
| ax1.set_title('title') |
| |
| |
| |
| left, bottom, width, height = 0.2, 0.6, 0.25, 0.25 |
| ax2 = fig.add_axes([left, bottom, width, height]) |
| ax2.plot(y, x, 'b') |
| ax2.set_xlabel('x') |
| ax2.set_ylabel('y') |
| ax2.set_title('title inside 1') |
| |
| |
| |
| |
| plt.axes([0.6, 0.2, 0.25, 0.25]) |
| plt.plot(y[::-1], x, 'g') |
| plt.xlabel('x') |
| plt.ylabel('y') |
| plt.title('title inside 2') |
| |
| plt.show() |

主次坐标
| import matplotlib.pyplot as plt |
| import numpy as np |
| |
| x = np.arange(0, 10, 0.1) |
| y1 = 0.05 * x**2 |
| y2 = -1 *y1 |
| |
| fig, ax1 = plt.subplots() |
| |
| # 反转ax1的y坐标 |
| ax2 = ax1.twinx() |
| ax1.plot(x, y1, 'g-') |
| ax2.plot(x, y2, 'b-') |
| |
| ax1.set_xlabel('X data') |
| ax1.set_ylabel('Y1 data', color='g') |
| ax2.set_ylabel('Y2 data', color='b') |
| |
| plt.show() |

参考资料
Matplotlib User's Guide
Matplotlib Python 画图教程 (莫烦Python)
GithubCode
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)