简体   繁体   English

如何在 matplotlib 条形图中的条形上方显示 Y 值?

[英]How do I display Y values above the bars in a matplotlib barchart?

I am generating a bar chart from a dataframe, I want to remove the Y axis labels and display them above the bars.我正在从 dataframe 生成条形图,我想删除 Y 轴标签并将它们显示在条形上方。 How can I achieve this?我怎样才能做到这一点?
This is my code so far:到目前为止,这是我的代码:

ax = rounded_df.plot(kind='bar',
                     figsize=(20, 8),
                     width=0.8,
                     color=['#5cb85c', '#5bc0de', '#d9534f']
                     )
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.legend(fontsize=14)
plt.title('Percentage of Respodents\' Interest in Data Science Areas', fontsize=16)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)

Here is my graph as it stands: Graph这是我的图表:图表

And here is the data:这是数据:

                            Very_interested Somewhat_interested Not_interested
Data_Analysis/Statistics    72.95           21.36               3.31
Machine_Learning            75.59           19.88               2.69
Data_Visualization          60.01           32.87               4.57
Big_Data_(Spark/Hadoop)     59.65           32.65               5.69
Deep_Learning               56.56           34.48               6.09
Data_Journalism             19.21           48.41               27.32

using ax.patches you can achieve it.使用ax.patches你可以实现它。

This will do:这将做:

for p in ax.patches:
        ax.annotate(str(round(p.get_height(),2)), (p.get_x() * 1.005, p.get_height() * 1.005),color='green')

Try:尝试:

sample data:样本数据:

    1q1         1q2         1q3         1q4
Cust / Month                
AA  62264.65    103189.45   94989.70    86212.37
AB  3330.47     0.00        0.00        0.00
AC  36580.00    36113.10    37527.84    32241.18
AD  36803.13    0.00        0.00        0.00
AE  44200.66    0.00        0.00        0.00
AF  12971.52    13697.76    13257.60    11931.84

Sample Code:示例代码:

customer_count = [38.00, 30.00, 30.00, 20.00]

ax = res.T.plot(width=0.8, kind='bar',y=['Quarter Total'],figsize=(10,5))
for rect, value in zip(ax.patches, customer_count):
    if value != 0:
        h = rect.get_height() /2.
        w = rect.get_width() /2.
        x, y = rect.get_xy()
        ax.text(x+w, y+h,value, horizontalalignment='center', verticalalignment='center', color='white',rotation=0)

for p in ax.patches:
    ax.annotate(str(round(p.get_height(),2)), (p.get_x() * 1.005, p.get_height() * 1.005),color='green')
    
plt.title('expediture data Quarter Wise')

在此处输入图像描述



df:东风:

    a   b   c   d
a1  66  92  98  17
a2  83  57  86  97
a3  96  47  73  32

ax = df.T.plot(width=0.8, kind='bar',y=df.columns,figsize=(10,5))
for p in ax.patches:
    ax.annotate(str(round(p.get_height(),2)), (p.get_x() * 1.005, p.get_height() * 1.005),color='green')
  
ax.axes.get_yaxis().set_ticks([])

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Matplotlib 中条形图中条形图上方的值 - Values above the bars in a barchart in Matplotlib 如何使用 matplotlib 在此条形图的相应条形上方显示这些值? 我的尝试不起作用 - How do I display these values above their respective bars on this bar chart with matplotlib? My attempts are not working 根据 y 轴上的值对条形图中的条形进行排序 - Sorting the bars in the barchart based on the values in y axis Python Matplotlib-如何在条形图中的y轴上设置值 - Python Matplotlib - how to set values on y axis in barchart 如何在 python 的 matplotlib 的条形图中为某些条形着色? - How to color certain bars in barchart of matplotlib in python? 如何在 Y 轴和 Matplotlib 中的开始和结束垂直条之间留出一些空间 - How to do I give some space between Y axis and my starting and ending vertical bars in Matplotlib Matplotlib:如果绘制了numpy.nan值,则不会显示堆叠的条 - Matplotlib: stacked bars do not display if numpy.nan values are plotted 如何在matplotlib的条形图中反转刻度的顺序? - How do I reverse the order of ticks on a barchart in matplotlib? 如何将文字放在 matplotlib 中的横条 plot 上方? - How to fit the text above the bars plot in matplotlib? 如何控制在matplotlib 2.1.0 barh图表中显示条的顺序? - How do I control the order to display bars in a matplotlib 2.1.0 barh chart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM