Python 按顺序显示图形而不仅仅是最后一个
当我在IPython上使用熊猫进行以下操作时,它只显示我绘制的最后一张图片,有没有办法让它们在IPython上按顺序显示Python 按顺序显示图形而不仅仅是最后一个,python,pandas,matplotlib,Python,Pandas,Matplotlib,当我在IPython上使用熊猫进行以下操作时,它只显示我绘制的最后一张图片,有没有办法让它们在IPython上按顺序显示 def drawBar(colName): df1=df[colName].value_counts().plot(kind='bar', title=colName) drawBar("myBiscuit") drawBar("myBedRoom") ...(many more drawBar) 要在笔记本中绘制图形,可以使用IPython magic%matp
def drawBar(colName):
df1=df[colName].value_counts().plot(kind='bar', title=colName)
drawBar("myBiscuit")
drawBar("myBedRoom")
...(many more drawBar)
要在笔记本中绘制图形,可以使用IPython magic
%matplotlib inline
(a) 逐个绘制每个图形:
您需要为每个图形调用plt.show()
。这将返回IPython中的一长串绘图
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
%matplotlib inline
colNames = "ABCDEFGHI"
x = np.random.randint(0,5, size=(10, 9))
df = pd.DataFrame(x, columns=[letter for letter in colNames])
def drawBar(colName):
df1=df[colName].value_counts().plot(kind='bar', title=colName)
for i in range(9):
drawBar(colNames[i])
plt.show()
(b) 使用子图。
可以使用plt.subplot()
创建多个子批次。然后使用pandas plotting函数的ax
关键字参数,在指定轴上创建图形
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
%matplotlib inline
colNames = "ABCDEFGHI"
x = np.random.randint(0,5, size=(10, 9))
df = pd.DataFrame(x, columns=[letter for letter in colNames])
fig, axes = plt.subplots(3,3)
def drawBar(colName, ax):
df1=df[colName].value_counts().plot(kind='bar', title=colName, ax=ax)
for i, ax in enumerate(axes.flatten()):
drawBar(colNames[i], ax)
plt.tight_layout()
您想在显示下一个窗口之前暂停一下,还是采用交互模式-即等待上一个窗口关闭?@doctorlove只需一次显示所有窗口或许多子窗口?@doctorlove许多绘图或子绘图如果没有更多信息,我想我帮不上忙,但有两件事。首先,
plot
命令有一个suplot参数,可用于生成子图。第二,如果
matplotlib.is_interactive()`报告为False,则图形应全部显示(每个绘图一个)