显示先前创建的熊猫图
假设我在jupyter笔记本中创建了一个条图:
import pandas as pd
import matplotlib.pyplot as plt
speed = [0.1, 17.5, 40, 48, 52, 69, 88]
lifespan = [2, 8, 70, 1.5, 25, 12, 28]
index = ["snail", "pig", "elephant", "rabbit", "giraffe", "coyote", "horse"]
df = pd.DataFrame({"speed": speed, "lifespan": lifespan}, index=index)
plot = df.plot(kind='bar', stacked=True)
这显示了我的图表。
然后,在下一个单元格中,我进行了修改,例如添加数据标签:
for bar in plot.patches:
height = bar.get_height()
width = bar.get_width()
x = bar.get_x()
y = bar.get_y()
label_text = height
label_x = x + width / 2
label_y = y + height / 2
if label_text != 0:
plot.text(
label_x,
label_y,
int(label_text),
ha="center",
va="center",
color="white",
fontweight="bold",
)
现在,如何显示情节再次? plt.show()
什么也没返回。
Say I create a bar plot in a Jupyter notebook:
import pandas as pd
import matplotlib.pyplot as plt
speed = [0.1, 17.5, 40, 48, 52, 69, 88]
lifespan = [2, 8, 70, 1.5, 25, 12, 28]
index = ["snail", "pig", "elephant", "rabbit", "giraffe", "coyote", "horse"]
df = pd.DataFrame({"speed": speed, "lifespan": lifespan}, index=index)
plot = df.plot(kind='bar', stacked=True)
This shows my chart.
Then, in the next cell, I make a modification, e.g. adding data labels:
for bar in plot.patches:
height = bar.get_height()
width = bar.get_width()
x = bar.get_x()
y = bar.get_y()
label_text = height
label_x = x + width / 2
label_y = y + height / 2
if label_text != 0:
plot.text(
label_x,
label_y,
int(label_text),
ha="center",
va="center",
color="white",
fontweight="bold",
)
Now, how can I show the plot again? plt.show()
returns nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需再次显示图:
作为替代方案,您可以使用
plot.get_figure()
。
Just show the figure again:
As an alternative, you can use
plot.get_figure()
.