【问题标题】:Create a bar graph with Ploty express using a python dict使用 python dict 使用 Plotly express 创建条形图
【发布时间】:2021-10-25 18:31:11
【问题描述】:

我正在尝试使用已过滤的数据以 plotly express 创建一个条形图,在 python 中看起来像 {item_a: 1, item_b: 33, item_c: 47}。它没有索引。如何将这些数据制作成条形图,在 x 轴上显示 item_*,在 y 轴上显示总和?

【问题讨论】:

  • 你可以使用matplotlib import matplotlib.pyplot as plt data = {'item_a': 1, 'item_b': 33, 'item_c': 47} plt.bar(data.keys(), data.values())

标签: python plotly bar-chart


【解决方案1】:

如果你改用 graph_objects 会更容易

import plotly.graph_objects as go

data = {"item_a": 1, "item_b": 33, "item_c": 47}

fig = go.Figure()
fig.add_trace(go.Bar(x=list(data.keys()), y=list(data.values())))
fig.show()

【讨论】:

    【解决方案2】:

    情节表达最简单的输入是数据框。只需创建一个。

    import pandas as pd
    import plotly.express as px
    
    data = {"item_a": 1, "item_b": 33, "item_c": 47}
    
    px.bar(pd.DataFrame(data, index=["key"]).T, y="key")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-20
      • 2021-03-09
      • 2021-05-18
      • 2021-07-21
      • 1970-01-01
      • 2020-09-05
      • 2021-11-28
      • 2020-02-28
      相关资源
      最近更新 更多