本文介绍了如何绘制和注释分组条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了一个关于 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 中的堆栈条形图并为每个部分添加标签
- 如何在条形图上添加多个注释
- 来自 JohanC 的评论,w = 0.8/3 将解决这个问题,给定当前代码.
- 但是,使用 pandas.DataFrame.plot
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']/2233Using 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.
- 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
- 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
更多推荐
如何绘制和注释分组条形图
发布评论