Plotly 动画:折线图不可见(Python)

发布于 2025-01-09 04:00:39 字数 973 浏览 1 评论 0原文

我正在 Jupyter Notebook 中工作,试图显示动画折线图。 (看到它是从左到右绘制的。)

但是,当图表容器和所有控件都显示时,图表不会显示。就好像这条线是看不见的。

这是我的代码:

import pandas as pd
import plotly.express as px
import plotly.io as pio


data = [['Year', 'Country', 'Output', 'Investment', 'Depreciation'], ['2020', 'Netherlands', 1, 2, 1], ['2021', 'Netherlands', 2, 2, 1], ['2022', 'Netherlands', 3, 2, 1], ['2023', 'Netherlands', 4, 2, 1]]
df = pd.DataFrame(data[1:], columns=data[0])

pio.renderers.default = 'notebook'
px.line(df, 
        x='Year', 
        y='Output', 
        color='Country',
        title='Macroeconomic variables over time',
        range_x=[df['Year'].iloc[0], df['Year'].iloc[-1]],
        range_y=[0, max(df['Output']) * 1.25],
        animation_frame='Year')

给出以下图表(在 VSCode 和 Jupyter Notebook 中): 隐形线Chart

关于为什么会发生这种情况有什么想法吗?

I am working in a Jupyter Notebook trying to display a line chart being animated. (Seeing it being plotted from left to right.)

However, the chart doesn't show up while the chart container and all controls do show up. It is like the line is invisible.

This is my code:

import pandas as pd
import plotly.express as px
import plotly.io as pio


data = [['Year', 'Country', 'Output', 'Investment', 'Depreciation'], ['2020', 'Netherlands', 1, 2, 1], ['2021', 'Netherlands', 2, 2, 1], ['2022', 'Netherlands', 3, 2, 1], ['2023', 'Netherlands', 4, 2, 1]]
df = pd.DataFrame(data[1:], columns=data[0])

pio.renderers.default = 'notebook'
px.line(df, 
        x='Year', 
        y='Output', 
        color='Country',
        title='Macroeconomic variables over time',
        range_x=[df['Year'].iloc[0], df['Year'].iloc[-1]],
        range_y=[0, max(df['Output']) * 1.25],
        animation_frame='Year')

Giving the following chart (in both VSCode and Jupyter Notebooks):
Invisible line chart

Any ideas on why this happens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

丶视觉 2025-01-16 04:00:39
  • 从根本上讲,您需要考虑数据的维度。假设每有一行,并且您使用animation_frame,则每条轨迹中只有一个点。这不会使一条线
  • 模拟一个稍微不同的数据帧,该数据帧具有Decade并用作animation_frame。这意味着有 10 行,因此每条迹线有 10 个点组成一条线。
import pandas as pd
import plotly.express as px
import numpy as np

data = [
    ["Year", "Country", "Output", "Investment", "Depreciation"],
    ["2020", "Netherlands", 1, 2, 1],
    ["2021", "Netherlands", 2, 2, 1],
    ["2022", "Netherlands", 3, 2, 1],
    ["2023", "Netherlands", 4, 2, 1],
]
df = pd.DataFrame(data[1:], columns=data[0])
df = pd.DataFrame(
    {
        c: list(range(2000 if i == 0 else 1, 2050 if i == 0 else 51, 1))
        if c in ["Year", "Output"]
        else np.full(50, i if c != "Country" else "Netherlands")
        for i, c in enumerate(data[0])
    }
).assign(Decade=lambda d: d["Year"] // 10 - 200)

px.line(
    df,
    x="Year",
    y="Output",
    color="Country",
    title="Macroeconomic variables over time",
    range_x=[df["Year"].iloc[0], df["Year"].iloc[-1]],
    range_y=[0, max(df["Output"]) * 1.25],
    animation_frame="Decade",
)

输入图片此处描述

  • fundamentally you need to think about the dimensions of your data. Given there is one row per Year and you are using as animation_frame each trace will have only one point in it. This does not make a line
  • have simulated a slightly different data frame, that has Decade and used as animation_frame. This then means there are 10 rows, hence 10 points per trace that makes a line.
import pandas as pd
import plotly.express as px
import numpy as np

data = [
    ["Year", "Country", "Output", "Investment", "Depreciation"],
    ["2020", "Netherlands", 1, 2, 1],
    ["2021", "Netherlands", 2, 2, 1],
    ["2022", "Netherlands", 3, 2, 1],
    ["2023", "Netherlands", 4, 2, 1],
]
df = pd.DataFrame(data[1:], columns=data[0])
df = pd.DataFrame(
    {
        c: list(range(2000 if i == 0 else 1, 2050 if i == 0 else 51, 1))
        if c in ["Year", "Output"]
        else np.full(50, i if c != "Country" else "Netherlands")
        for i, c in enumerate(data[0])
    }
).assign(Decade=lambda d: d["Year"] // 10 - 200)

px.line(
    df,
    x="Year",
    y="Output",
    color="Country",
    title="Macroeconomic variables over time",
    range_x=[df["Year"].iloc[0], df["Year"].iloc[-1]],
    range_y=[0, max(df["Output"]) * 1.25],
    animation_frame="Decade",
)

enter image description here

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