相关疑难解决方法(0)

如何用pyplot.barh()显示每个栏上栏的值?

我生成了条形图,如何在每个条形图上显示条形图的值?

目前情节:

在此输入图像描述

我想要得到的:

在此输入图像描述

我的代码:

import os
import numpy as np
import matplotlib.pyplot as plt

x = [u'INFO', u'CUISINE', u'TYPE_OF_PLACE', u'DRINK', u'PLACE', u'MEAL_TIME', u'DISH', u'NEIGHBOURHOOD']
y = [160, 167, 137, 18, 120, 36, 155, 130]

fig, ax = plt.subplots()    
width = 0.75 # the width of the bars 
ind = np.arange(len(y))  # the x locations for the groups
ax.barh(ind, y, width, color="blue")
ax.set_yticks(ind+width/2)
ax.set_yticklabels(x, minor=False)
plt.title('title')
plt.xlabel('x')
plt.ylabel('y')      
#plt.show()
plt.savefig(os.path.join('test.png'), dpi=300, format='png', bbox_inches='tight') # use format='svg' or 'pdf' for vectorial pictures
Run Code Online (Sandbox Code Playgroud)

python matplotlib bar-chart

73
推荐指数
7
解决办法
17万
查看次数

matplotlib中的堆栈条形图并为每个部分添加标签(和建议)

我试图在matplotlib中复制以下图像,似乎是我唯一的选择.虽然看起来你不能堆叠barh图,所以我不知道该怎么做

在此输入图像描述

如果你知道一个更好的python库来绘制这种东西,请告诉我.

这就是我可以想到的一切:

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()
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

python matplotlib

12
推荐指数
2
解决办法
3万
查看次数

在堆叠的条形图中添加标签

我正在绘制某些类别中各个办公室的交叉表。我想放一张水平堆叠的条形图,在其中标记每个办公室及其值。

这是一些示例代码:

df = pd.DataFrame({'office1': pd.Series([1,np.nan,np.nan], index=['catA', 'catB', 'catC']),
              'office2': pd.Series([np.nan,8,np.nan], index=['catA', 'catB', 'catC']),
              'office3': pd.Series([12,np.nan,np.nan], index=['catA', 'catB', 'catC']),
              'office4': pd.Series([np.nan,np.nan,3], index=['catA', 'catB', 'catC']),
              'office5': pd.Series([np.nan,5,np.nan], index=['catA', 'catB', 'catC']),
              'office6': pd.Series([np.nan,np.nan,7], index=['catA', 'catB', 'catC']),
              'office7': pd.Series([3,np.nan,np.nan], index=['catA', 'catB', 'catC']),
              'office8': pd.Series([np.nan,np.nan,11], index=['catA', 'catB', 'catC']),
              'office9': pd.Series([np.nan,6,np.nan], index=['catA', 'catB', 'catC']),
              })
ax = df.plot.barh(title="Office Breakdown by Category", legend=False, figsize=(10,7), stacked=True)
Run Code Online (Sandbox Code Playgroud)

这给了我一个很好的起点:

堆积条形图示例

但是,我想拥有的是: 带标签的条形图

经过研究,我想到了以下代码,可以在“类别”轴上正确排列标签:

def annotateBars(row, ax=ax):
    for col in row.index:
        value = row[col]
        if (str(value) != 'nan'):
            ax.text(value/2, labeltonum(row.name), col+","+str(value))

def …
Run Code Online (Sandbox Code Playgroud)

python matplotlib pandas

3
推荐指数
1
解决办法
2853
查看次数

标签 统计

matplotlib ×3

python ×3

bar-chart ×1

pandas ×1