简体   繁体   English

Plotly:如何为离散/分类变量创建频率 plot?

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

I tried exploring everything in their website, but there is nothing regardless this ( https://plotly.com/python/v3/frequency-counts/ and https://plotly.com/python/v3/discrete-frequency/ won't solve my issue).我尝试探索他们网站上的所有内容,但无论如何都没有( https://plotly.com/python/v3/frequency-counts/https://plotly.com/python/v3/discrete-frequency/ 不会t解决我的问题)。 I wanted to plot a graph just like seaborn countplot ( https://seaborn.pydata.org/generated/seaborn.countplot.html ). I wanted to plot a graph just like seaborn countplot ( https://seaborn.pydata.org/generated/seaborn.countplot.html ).

I have this dataset:我有这个数据集:

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

I just wanted a barplot with CA, GE and CO in the x-axis and 3, 2 and 2 in the y-axis, respectively.我只想要一个条形图,x 轴为 CA、GE 和 CO,y 轴分别为 3、2 和 2。

You only need to groupby the state first and then use count just like so:您只需要先按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()

And it returns the following graph:它返回以下图表:

在此处输入图像描述

If you set plotly as your plotting backend for pandas , you can first group your data and do:如果将 plotly 设置为 pandas 的绘图后端,则可以首先对数据进行分组并执行以下操作:

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

在此处输入图像描述

Complete snippet完整的片段

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')

But if you would like a setup that you can expand a bit more on, I would use px.bar like so:但是,如果您想要一个可以进一步扩展的设置,我会像这样使用px.bar

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM