显示先前创建的熊猫图

发布于 2025-02-13 09:59:04 字数 1179 浏览 0 评论 0原文

假设我在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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒时光 2025-02-20 09:59:04

只需再次显示图:

plot.figure

”在此处输入图像描述”

作为替代方案,您可以使用 plot.get_figure()

Just show the figure again:

plot.figure

enter image description here

As an alternative, you can use plot.get_figure().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文