如何绘制和注释分组条形图

编程入门 行业动态 更新时间:2024-10-27 13:34:03
本文介绍了如何绘制和注释分组条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到了一个关于 Python 中 matplotlib 的棘手问题.我想创建一个包含多个代码的分组条形图,但图表出错了.你能给我一些建议吗?代码如下.

将 numpy 导入为 np将熊猫作为pd导入文件="s3-api.us-geo.objectstorage.softlayer/cf-courses-data/CognitiveClass/DV0101EN/labs/coursera/Topic_Survey_Assignment.csv"df = pd.read_csv(file,index_col = 0)df.sort_values(by = ['非常感兴趣'],轴= 0,升序= False,inplace = True)df['很感兴趣']=df['很感兴趣']/2233df['有点感兴趣']=df['有点感兴趣']/2233df ['不感兴趣'] = df ['不感兴趣']/2233dfdf_chart=df.round(2)df_chart标签=['数据分析/统计','机器学习','数据可视化',大数据(Spark/Hadoop)"、深度学习"、数据新闻"]very_interested = df_chart ['非常感兴趣']有点感兴趣=df_chart['有点感兴趣']not_interested = df_chart ['不感兴趣']x=np.arange(len(标签))w=0.8fig,ax = plt.subplots(figsize =(20,8))rects1 = ax.bar(x-w,非常感兴趣,w,label ='非常感兴趣',color ='#5cb85c')rects2=ax.bar(x,somewhat_interested,w,label='有点感兴趣',color='#5bc0de')rects3=ax.bar(x+w,not_interested,w,label='不感兴趣',color='#d9534f')ax.set_ylabel('百分比',fontsize=14)ax.set_title(受访者对不同数据科学领域的兴趣百分比",字体大小=16)ax.set_xticks(x)ax.set_xticklabels(标签)ax.legend(字体大小=14)def autolabel(rects):文本"在* rects *中的每个条上方附加一个文本标签,以显示其高度.对于 rect 中的 rect:高度= rect.get_height()ax.annotate('{}'.format(height),xy=(rect.get_x() + rect.get_width()/3, 高度),xytext = {0,3),#3点垂直偏移textcoords =偏移点",ha ='center',va ='bottom')自动标签(rects1)自动标签(rects2)自动标签(rects3)fig.tight_layout()plt.show()

这个代码模块的输出真的是一团糟.但是我期望的应该看起来像图片中的条形图.您能告诉我代码中哪一点不正确吗?

解决方案

导入和DataFrame

将熊猫作为pd导入导入matplotlib.pyplot作为plt#给出以下代码来创建数据框文件="s3-api.us-geo.objectstorage.softlayer/cf-courses-data/CognitiveClass/DV0101EN/labs/coursera/Topic_Survey_Assignment.csv"df = pd.read_csv(file,index_col = 0)df.sort_values(by = ['非常感兴趣'],轴= 0,升序= False,inplace = True)df['很感兴趣']=df['很感兴趣']/2233df ['有兴趣的人'] = df ['有兴趣的人']/2233df['不感兴趣']=df['不感兴趣']/2233

使用自 matplotlib v3.4.2

  • 使用

    注释资源 - 来自 matplotlib v3.4.2
    • 在 matplotlib 条形图上添加值标签
    • 如何注释堆积条形图的每一段
    • 带有居中标签的堆叠条形图
    • 如何在海洋条形图中绘制和注释多个数据列
    • 如何使用汇总值注释海洋航海图
    • matplotlib 中的堆栈条形图并为每个部分添加标签
    • 如何在条形图上添加多个注释
    在 matplotlib v3.4.2 之前使用
    • 来自 JohanC 的评论,w = 0.8/3 将解决这个问题,给定当前代码.
    • 但是,使用 pandas.DataFrame.plot

    # 你的颜色颜色= ['#5cb85c','#5bc0de','#d9534f']# 带有注释的绘图可能更容易p1 = df.plot.bar(颜色=颜色,figsize =(20,8),ylabel =百分比",标题=受访者对不同数据科学领域的兴趣百分比")p1.set_xticklabels(p1.get_xticklabels(),旋转= 0)对于p1.patch中的p:p1.annotate(f'{p.get_height():0.2f}', (p.get_x() + p.get_width()/2., p.get_height()), ha = 'center', va = 'center', xytext = (0, 10), textcoords = '偏移点')

    I came across a tricky issue about the matplotlib in Python. I want to create a grouped bar chart with several codes, but the chart goes wrong. Could you please offer me some advice? The code is as follows.

    import numpy as np import pandas as pd file="s3-api.us-geo.objectstorage.softlayer/cf-courses-data/CognitiveClass/DV0101EN/labs/coursera/Topic_Survey_Assignment.csv" df=pd.read_csv(file,index_col=0) df.sort_values(by=['Very interested'], axis=0,ascending=False,inplace=True) df['Very interested']=df['Very interested']/2233 df['Somewhat interested']=df['Somewhat interested']/2233 df['Not interested']=df['Not interested']/2233 df df_chart=df.round(2) df_chart labels=['Data Analysis/Statistics','Machine Learning','Data Visualization', 'Big Data (Spark/Hadoop)','Deep Learning','Data Journalism'] very_interested=df_chart['Very interested'] somewhat_interested=df_chart['Somewhat interested'] not_interested=df_chart['Not interested'] x=np.arange(len(labels)) w=0.8 fig,ax=plt.subplots(figsize=(20,8)) rects1=ax.bar(x-w,very_interested,w,label='Very interested',color='#5cb85c') rects2=ax.bar(x,somewhat_interested,w,label='Somewhat interested',color='#5bc0de') rects3=ax.bar(x+w,not_interested,w,label='Not interested',color='#d9534f') ax.set_ylabel('Percentage',fontsize=14) ax.set_title("The percentage of the respondents' interest in the different data science Area", fontsize=16) ax.set_xticks(x) ax.set_xticklabels(labels) ax.legend(fontsize=14) def autolabel(rects): """Attach a text label above each bar in *rects*, displaying its height.""" for rect in rects: height = rect.get_height() ax.annotate('{}'.format(height), xy=(rect.get_x() + rect.get_width() / 3, height), xytext=(0, 3), # 3 points vertical offset textcoords="offset points", ha='center', va='bottom') autolabel(rects1) autolabel(rects2) autolabel(rects3) fig.tight_layout() plt.show()

    The output of this code module is really a mess. But what I expect should look like the bar chart in the picture. Could you please tell me which point is not correct in my codes?

    解决方案

    Imports and DataFrame

    import pandas as pd import matplotlib.pyplot as plt # given the following code to create the dataframe file="s3-api.us-geo.objectstorage.softlayer/cf-courses-data/CognitiveClass/DV0101EN/labs/coursera/Topic_Survey_Assignment.csv" df=pd.read_csv(file,index_col=0) df.sort_values(by=['Very interested'], axis=0,ascending=False,inplace=True) df['Very interested']=df['Very interested']/2233 df['Somewhat interested']=df['Somewhat interested']/2233 df['Not interested']=df['Not interested']/2233

    Using since matplotlib v3.4.2

    • Use matplotlib.pyplot.bar_label
    • See the matplotlib: Bar Label Demo page for additional formatting options.
      • Some formatting can be done with the fmt parameter, but more sophisticated formatting should be done with the labels parameter, as show in the bottom Demo example, and in How to add multiple annotations to a barplot.

    # your colors colors = ['#5cb85c', '#5bc0de', '#d9534f'] # plot with annotations is probably easier p1 = df.plot.bar(color=colors, figsize=(20, 8), ylabel='Percentage', title="The percentage of the respondents' interest in the different data science Area") p1.set_xticklabels(p1.get_xticklabels(), rotation=0) for p in p1.containers: p1.bar_label(p, fmt='%.2f', label_type='edge')

    Annotation Resources - from matplotlib v3.4.2
    • Adding value labels on a matplotlib bar chart
    • How to annotate each segment of a stacked bar chart
    • Stacked Bar Chart with Centered Labels
    • How to plot and annotate multiple data columns in a seaborn barplot
    • How to annotate a seaborn barplot with the aggregated value
    • stack bar plot in matplotlib and add label to each section
    • How to add multiple annotations to a barplot
    Using before matplotlib v3.4.2
    • The comment from JohanC, w = 0.8 / 3 will resolve the issue, given the current code.
    • However, generating the plot can be accomplished more easily with pandas.DataFrame.plot

    # your colors colors = ['#5cb85c', '#5bc0de', '#d9534f'] # plot with annotations is probably easier p1 = df.plot.bar(color=colors, figsize=(20, 8), ylabel='Percentage', title="The percentage of the respondents' interest in the different data science Area") p1.set_xticklabels(p1.get_xticklabels(), rotation=0) for p in p1.patches: p1.annotate(f'{p.get_height():0.2f}', (p.get_x() + p.get_width() / 2., p.get_height()), ha = 'center', va = 'center', xytext = (0, 10), textcoords = 'offset points')

更多推荐

如何绘制和注释分组条形图

本文发布于:2023-07-18 00:45:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139227.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   条形图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!