【问题标题】:how to create a bar chart in python with multiple x-axis如何在python中创建具有多个x轴的条形图
【发布时间】:2021-03-25 15:00:27
【问题描述】:

我有一个包含 3 列的数据集: BOROUGHS,COMPLAINT_DATE,违规

注意:日期格式如下:2010-01-30

我确实知道如何创建一个简单的条形图...像这样:

df.plot(kind="bar")

但是,我需要这样的东西:

这张图表告诉我 5 个行政区、投诉数量和年份。加上使用颜色。

首先,你是怎么做这样的事情的? 第二,这种图表有名字吗?比如,多条形图或类似的东西?

编辑:

紫色应该是第一个……在酒吧……但它说它有更多的罪行……

编辑:#2 另外...看看这个基于 2010 年和 2019 年的数字

编辑:#3 太小... 底部没有显示犯罪数量 谢谢,

【问题讨论】:

  • 紫色应该是第一个......读。每个部分代表一次违规计数,只有整个条形高度与 y 轴上的值相关联。
  • 这是堆积条的问题,很难区分条的每个部分的值。

标签: python pandas matplotlib seaborn bar-chart


【解决方案1】:
  • 数据需要按计数进行分组和聚合,然后转换为正确的形状。
    • 使用.dt 访问器从'complaint_date' 列中提取年份。
  • 有关所有可用参数,请参阅 pandas.DataFrame.plotpandas.DataFrame.plot.bar
import pandas as pd
import matplotlib.pyplot as plt

# sample data
data = {'boroughs': ['x', 'y', 'z', 'x', 'y', 'z', 'x', 'y', 'z', 'x', 'y', 'z', 'x'],
        'complaint_date': ['2020-11-1', '2020-11-1', '2020-11-1', '2019-11-1', '2019-11-1', '2019-11-1', '2020-11-1', '2020-11-1', '2020-11-1', '2019-11-1', '2019-11-1', '2019-11-1', '2019-11-1'],
        'offense': ['a', 'b', 'c', 'a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f', 'd']}

# create dataframe
df = pd.DataFrame(data)

# convert date column to datetime dtype
df.complaint_date = pd.to_datetime(df.complaint_date)

# groupby year and borough to get count of offenses
dfg = df.groupby([df.complaint_date.dt.year, 'boroughs']).boroughs.count().reset_index(name='count')

# display(dfg)
   complaint_date boroughs  count
0            2019        x      3
1            2019        y      2
2            2019        z      2
3            2020        x      2
4            2020        y      2
5            2020        z      2

# pivot into the correct form for stacked bar
dfp = dfg.pivot(index='complaint_date', columns='boroughs', values='count')

# display(dfp)
boroughs        x  y  z
complaint_date         
2019            3  2  2
2020            2  2  2

# plot
dfp.plot.bar(stacked=True, xlabel='Year Complaint Filed', ylabel='Volumn of Complaints')
plt.legend(title='Boroughs', bbox_to_anchor=(1.05, 1), loc='upper left')
plt.xticks(rotation=0)

回复评论

  • 回复AttributeError: 'Rectangle' object has no property 'xlabel'
  • pandas 可能需要更新;这是在版本 1.1.3 中运行的。
# plot
dfp.plot.bar(stacked=True)
plt.legend(title='Boroughs', bbox_to_anchor=(1.05, 1), loc='upper left')
plt.xlabel('Year Complaint Filed')
plt.ylabel('Volumn of Complaints')
plt.xticks(rotation=0)

比堆叠条更好的选择

  • 使用seaborn.barplot
  • 这将为每个条形的相对值提供更好的整体表示。
import seaborn as sns

# use dfg from above

# plot
fig, ax = plt.subplots(figsize=(6, 4))
sns.barplot(y='complaint_date', x='count', data=dfg, hue='boroughs', orient='h', ax=ax)

# use log scale since you have large numbers
plt.xscale('log')

# relocate the legend
plt.legend(title='Boroughs', bbox_to_anchor=(1.05, 1), loc='upper left')

  • 请参阅 questionquestion 将 x-tick 值的格式从指数更改为整数。

【讨论】:

  • 完美!谢谢!!! ....感谢您的帮助...它有效!
猜你喜欢
  • 1970-01-01
  • 2018-11-25
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多