Plotly:如何为 discrete/categorical 变量绘制频率图?

Plotly: How to make a frequency plot for discrete/categorical variables?

我尝试在他们的网站上探索所有内容,但无论如何都没有 (https://plotly.com/python/v3/frequency-counts/ and https://plotly.com/python/v3/discrete-frequency/ won't solve my issue). I wanted to plot a graph just like seaborn countplot (https://seaborn.pydata.org/generated/seaborn.countplot.html)。

我有这个数据集:

id      state       value
19292   CA          100
24592   CA          200
12492   GE          340
11022   GE          500
99091   CO          250
59820   CO          220
50281   CA          900

我只想要一个条形图,x 轴为 CA、GE 和 CO,y 轴分别为 3、2 和 2。

您只需要先 groupby state 然后使用 count 就像这样:

>>> import pandas as pd
>>> import matplotlib.pyplot as plt

>>> df
      id state  value
0  19292    CA    100
1  24592    CA    200
2  12492    GE    340
3  11022    GE    500
4  99091    CO    250
5  59820    CO    220
6  50281    CA    900

>>> new_df = df.groupby(["state"]).count().reset_index()
  state  id  value
0    CA   3      3
1    CO   2      2
2    GE   2      2
>>> new_df.plot.bar(x="state", y="value")
>>> plt.show()

它 returns 下图:

如果您设置plotly as your plotting backend for pandas,您可以先将数据分组并执行:

df.groupby(["state"]).count().reset_index().plot(x='state', y='value', kind='bar')

完整片段

import pandas as pd
pd.options.plotting.backend = "plotly"
df = pd.DataFrame({'id': {0: 19292, 1: 24592, 2: 12492, 3: 11022, 4: 99091, 5: 59820, 6: 50281},
                     'state': {0: 'CA', 1: 'CA', 2: 'GE', 3: 'GE', 4: 'CO', 5: 'CO', 6: 'CA'},
                     'value': {0: 100, 1: 200, 2: 340, 3: 500, 4: 250, 5: 220, 6: 900}})

df.groupby(["state"]).count().reset_index().plot(x='state', y='value', kind='bar')

但是如果您想要一个可以进一步扩展的设置,我会像这样使用 px.bar

dfg = df.groupby(["state"]).count()
fig = px.bar(dfg, x=dfg.index, y="value")
fig.show()