简体   繁体   中英

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

You only need to groupby the state first and then use count just like so:

>>> 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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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