首页 > 解决方案 > 使用 matplotlib 问题显示两个数字

问题描述

我试图在两个不同的情节中同时描绘两个人物。我只得到一个数字(特别是第一个数字)。

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
df = pd.read_excel("My excel.xlsx")
A1 = some value 
A2 = value 
A20 = value 
Z = value 

fig = plt.figure()
ax = fig.add_subplot()
ax.plot(Z,A1)
ax.plot(Z,A2)
ax.plot(Z,A3)
ax.plot(Z,A4)
ax.plot(Z,A5)
ax.plot(Z,A6)
ax.plot(Z,A20) 
plt.legend(loc = 'upper left')
plt.figure()
plt.show()

fig = plt.figure()
ax.plot = fig.add_subplot()
B1 = value 
B2 = value 
B3 = value 
B4 = value 
B20 = value 

ax.plt(Z,B1)
ax.plt(Z,B2)
ax.plt(Z,B20)
plt.legend(loc= 'upper left')
plt.figure()
plt.show()

我看过一个相关的帖子处理同样的问题,但无法得出结论。欢迎任何帮助。先感谢您!

标签: pythonmatplotlibsubplotfig

解决方案


这是一个可以帮助您创建多个子图的链接:https
://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html 我还建议使用 list 作为 plt.plot 的参数:
使用

ax.plt([Z, Z, Z], [B1, B2, B20])

代替

ax.plt(Z,B1)
ax.plt(Z,B2)
ax.plt(Z,B20)

推荐阅读