pandas Python创建条形图比较2组数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53182452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:08:09  来源:igfitidea点击:

Python Create Bar Chart Comparing 2 sets of data

pythonpandasbar-chart

提问by A Johnston

I have a notebook with 2* bar charts, one is winter data & one is summer data. I have counted the total of all the crimes and plotted them in a bar chart, using code:

我有一个带有 2* 条形图的笔记本,一个是冬季数据,一个是夏季数据。我计算了所有犯罪的总数并将它们绘制在条形图中,使用代码:

ax = summer["crime_type"].value_counts().plot(kind='bar')
plt.show()

Which shows a graph like:

它显示了一个图表,如:

enter image description here

在此处输入图片说明

I have another chart nearly identical, but for winter:

我有另一个几乎相同的图表,但对于冬天:

ax = winter["crime_type"].value_counts().plot(kind='bar')
plt.show()

And I would like to have these 2 charts compared against one another in the same bar chart (Where every crime on the x axis has 2 bars coming from it, one winter & one summer).

我想让这两个图表在同一个条形图中相互比较(x 轴上的每个犯罪都有 2 个条形来自它,一个冬天和一个夏天)。

I have tried, which is just me experimenting:

我试过了,这只是我在试验:

bx = (summer["crime_type"],winter["crime_type"]).value_counts().plot(kind='bar')
plt.show()

Any advice would be appreciated!

任何意见,将不胜感激!

回答by Charles Landau

The following generates dummies of your data and does the grouped bar chart you wanted:

以下生成您的数据的虚拟对象并生成您想要的分组条形图:

import random
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

s = "Crime Type Summer|Crime Type Winter".split("|")

# Generate dummy data into a dataframe
j = {x: [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]
                       ) for j in range(300)] for x in s}
df = pd.DataFrame(j)

index = np.arange(5)
bar_width = 0.35

fig, ax = plt.subplots()
summer = ax.bar(index, df["Crime Type Summer"].value_counts(), bar_width,
                label="Summer")

winter = ax.bar(index+bar_width, df["Crime Type Winter"].value_counts(),
                 bar_width, label="Winter")

ax.set_xlabel('Category')
ax.set_ylabel('Incidence')
ax.set_title('Crime incidence by season, type')
ax.set_xticks(index + bar_width / 2)
ax.set_xticklabels(["ASB", "Violence", "Theft", "Public Order", "Drugs"])
ax.legend()

plt.show()

With this script I got:

有了这个脚本,我得到了:

this image

这个图片

You can check out the demo in the matplotlib docs here: https://matplotlib.org/gallery/statistics/barchart_demo.html

您可以在此处查看 matplotlib 文档中的演示:https: //matplotlib.org/gallery/statistics/barchart_demo.html

The important thing to note is the index!

需要注意的重要一点是索引!

index = np.arange(5) # Set an index of n crime types
...
summer = ax.bar(index, ...)
winter = ax.bar(index+bar_width, ...)
...
ax.set_xticks(index + bar_width / 2)

These are the lines that arrange the bars on the horizontal axis so that they are grouped together.

这些是在水平轴上排列条形的线,以便它们组合在一起。