pandas 如何在matplotlib中按不同组绘制直方图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31257540/
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-13 23:34:53  来源:igfitidea点击:

How to plot a histogram by different groups in matplotlib?

pythonpandasmatplotlibplothistogram

提问by Zelong

I have a table like:

我有一张像:

value    type
10       0
12       1
13       1
14       2

Generate a dummy data:

生成虚拟数据:

import numpy as np

value = np.random.randint(1, 20, 10)
type = np.random.choice([0, 1, 2], 10)

I want to accomplish a task in Python 3 with matplotlib (v1.4):

我想用 matplotlib (v1.4) 在 Python 3 中完成一项任务:

  • plot a histogram of value
  • group by type, i.e. use different colors to differentiate types
  • the position of the "bars" should be "dodge", i.e. side by side
  • since the range of value is small, I would use identityfor bins, i.e. the width of a bin is 1
  • 绘制直方图 value
  • group by type,即使用不同的颜色来区分类型
  • “酒吧”的位置应该是“躲避”,即并排
  • 由于值的范围很小,我将identity用于垃圾箱,即垃圾箱的宽度为 1

The questionsare:

问题是:

  • how to assign colors to bars based on the values of typeand draw colors from colormap (e.g. Accentor other cmap in matplotlib)? I don't want to use named color(i.e. 'b', 'k', 'r')
  • the bars in my histogram overlap each other, how to "dodge" the bars?
  • 如何根据type颜色图(例如Accent或 matplotlib 中的其他 cmap)的值和绘制颜色为条形分配颜色?我不想使用命名颜色(即'b', 'k', 'r'
  • 我的直方图中的条形相互重叠,如何“躲避”条形?


Note(lest the post will be down voted and deemed to be "naive")

注意(以免帖子被否决并被视为“幼稚”)

  1. I have tried on Seaborn, matplotlib and pandas.plotfor two hours and failed to get the desired histogram.
  2. I read the examples and Users' Guide of matplotlib. Surprisingly, I found no tutorial about how to assign colors from colormap.
  3. I have searched on Google but failed to find a succinct example.
  4. I guess one could accomplish the task with matplotlib.pyplot, without import a bunch of modules such as matplotlib.cm, matplotlib.colors.
  1. 我已经尝试了 Seaborn、matplotlib 和pandas.plot两个小时,但未能获得所需的直方图。
  2. 我阅读了 matplotlib 的示例和用户指南。令人惊讶的是,我没有找到关于如何从颜色图中分配颜色的教程。
  3. 我在谷歌上搜索过,但没有找到一个简洁的例子。
  4. 我想可以使用 完成任务matplotlib.pyplot,而无需导入一堆模块,例如matplotlib.cm, matplotlib.colors

回答by maxymoo

For your first question, we can create a dummy column equal to 1, and then generate counts by summing this column, grouped by value and type.

对于您的第一个问题,我们可以创建一个等于 1 的虚拟列,然后通过对按值和类型分组的此列求和来生成计数。

For your second question you can pass the colormap directly into plotusing the colormapparameter:

对于您的第二个问题,您可以plot使用colormap参数将颜色图直接传递给:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import seaborn
seaborn.set() #make the plots look pretty

df = pd.DataFrame({'value': value, 'type': type})
df['dummy'] = 1
ag = df.groupby(['value','type']).sum().unstack()
ag.columns = ag.columns.droplevel()

ag.plot(kind = 'bar', colormap = cm.Accent, width = 1)
plt.show()

enter image description here

在此处输入图片说明