matplotlib 中的堆栈条形图并为每个部分添加标签 [英] stack bar plot in matplotlib and add label to each section

查看:68
本文介绍了matplotlib 中的堆栈条形图并为每个部分添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 matplotlib 中复制以下图像,看来 barh 是我唯一的选择.尽管看来您无法堆叠 barh 图,所以我不知道该怎么做

如果您知道有更好的python库可以绘制此类内容,请告诉我.

这是我能想到的所有开始:

import matplotlib.pyplot as plt;plt.rcdefaults()将numpy导入为np导入matplotlib.pyplot作为pltpeople = ('A','B','C','D','E','F','G','H')y_pos = np.arange(len(people))底部数据 = 3 + 10 * np.random.rand(len(people))topdata = 3 + 10 * np.random.rand(len(people))无花果= plt.figure(figsize =(10,8))ax = fig.add_subplot(111)ax.barh(y_pos,bottomdata,color ='r',align ='center')ax.barh(y_pos,topdata,color ='g',align ='center')ax.set_yticks(y_pos)ax.set_yticklabels(people)ax.set_xlabel('Distance')plt.show()

然后,我将不得不使用 ax.text 分别添加标签,这会很乏味.理想情况下,我只想指定要插入的部分的宽度,然后使用我选择的字符串更新该部分的中心.外面的标签(例如 3800)我可以稍后添加,它主要是在条形部分本身上的标签,并以一种很好的方式创建了这种堆叠方法,我遇到了问题.您甚至可以以任何方式指定距离",即颜色范围吗?

解决方案

Edit 2:用于更多异构数据.(我已经放弃了上述方法,因为我发现每个系列使用相同数量的记录更常见)

回答问题的两个部分:

a) barh 返回其绘制的所有修补程序的句柄容器.您可以使用补丁的坐标来辅助文本位置.

b) 关注 .


I am trying to replicate the following image in matplotlib and it seems barh is my only option. Though it appears that you can't stack barh graphs so I don't know what to do

If you know of a better python library to draw this kind of thing, please let me know.

This is all I could come up with as a start:

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

people = ('A','B','C','D','E','F','G','H')
y_pos = np.arange(len(people))
bottomdata = 3 + 10 * np.random.rand(len(people))
topdata = 3 + 10 * np.random.rand(len(people))
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)
ax.barh(y_pos, bottomdata,color='r',align='center')
ax.barh(y_pos, topdata,color='g',align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.set_xlabel('Distance')

plt.show()

I would then have to add labels individually using ax.text which would be tedious. Ideally I would like to just specify the width of the part to be inserted then it updates the center of that section with a string of my choosing. The labels on the outside (e.g. 3800) I can add myself later, it is mainly the labeling over the bar section itself and creating this stacked method in a nice way I'm having problems with. Can you even specify a 'distance' i.e. span of color in any way?

解决方案

Edit 2: for more heterogeneous data. (I've left the above method since I find it more usual to work with the same number of records per series)

Answering the two parts of the question:

a) barh returns a container of handles to all the patches that it drew. You can use the coordinates of the patches to aid the text positions.

b) Following these two answers to the question that I noted before (see Horizontal stacked bar chart in Matplotlib), you can stack bar graphs horizontally by setting the 'left' input.

and additionally c) handling data that is less uniform in shape.

Below is one way you could handle data that is less uniform in shape is simply to process each segment independently.

import numpy as np
import matplotlib.pyplot as plt

# some labels for each row
people = ('A','B','C','D','E','F','G','H')
r = len(people)

# how many data points overall (average of 3 per person)
n = r * 3

# which person does each segment belong to?
rows = np.random.randint(0, r, (n,))
# how wide is the segment?
widths = np.random.randint(3,12, n,)
# what label to put on the segment (xrange in py2.7, range for py3)
labels = range(n)
colors ='rgbwmc'

patch_handles = []

fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)



left = np.zeros(r,)
row_counts = np.zeros(r,)

for (r, w, l) in zip(rows, widths, labels):
    print r, w, l
    patch_handles.append(ax.barh(r, w, align='center', left=left[r],
        color=colors[int(row_counts[r]) % len(colors)]))
    left[r] += w
    row_counts[r] += 1
    # we know there is only one patch but could enumerate if expanded
    patch = patch_handles[-1][0] 
    bl = patch.get_xy()
    x = 0.5*patch.get_width() + bl[0]
    y = 0.5*patch.get_height() + bl[1]
    ax.text(x, y, "%d%%" % (l), ha='center',va='center')
  
y_pos = np.arange(8)
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.set_xlabel('Distance')

plt.show()

Which produces a graph like this , with a different number of segments present in each series.

Note that this is not particularly efficient since each segment used an individual call to ax.barh. There may be more efficient methods (e.g. by padding a matrix with zero-width segments or nan values) but this likely to be problem-specific and is a distinct question.


Edit: updated to answer both parts of the question.

import numpy as np
import matplotlib.pyplot as plt

people = ('A','B','C','D','E','F','G','H')
segments = 4

# generate some multi-dimensional data & arbitrary labels
data = 3 + 10* np.random.rand(segments, len(people))
percentages = (np.random.randint(5,20, (len(people), segments)))
y_pos = np.arange(len(people))

fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)

colors ='rgbwmc'
patch_handles = []
left = np.zeros(len(people)) # left alignment of data starts at zero
for i, d in enumerate(data):
    patch_handles.append(ax.barh(y_pos, d, 
      color=colors[i%len(colors)], align='center', 
      left=left))
    # accumulate the left-hand offsets
    left += d
    
# go through all of the bar segments and annotate
for j in range(len(patch_handles)):
    for i, patch in enumerate(patch_handles[j].get_children()):
        bl = patch.get_xy()
        x = 0.5*patch.get_width() + bl[0]
        y = 0.5*patch.get_height() + bl[1]
        ax.text(x,y, "%d%%" % (percentages[i,j]), ha='center')

ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.set_xlabel('Distance')

plt.show()

You can achieve a result along these lines (note: the percentages I used have nothing to do with the bar widths, as the relationship in the example seems unclear):

See Horizontal stacked bar chart in Matplotlib for some ideas on stacking horizontal bar plots.


这篇关于matplotlib 中的堆栈条形图并为每个部分添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆