Python 如何为条形图设置文字注释的水平对齐方式
我用这段代码绘制了一些数据:Python 如何为条形图设置文字注释的水平对齐方式,python,pandas,matplotlib,seaborn,bar-chart,Python,Pandas,Matplotlib,Seaborn,Bar Chart,我用这段代码绘制了一些数据: plt.rcdefaults() fig, ax = plt.subplots(figsize=(10,6)) # Example data people = data_year[data_year.columns[1]] y_pos = np.arange(len(people)) #performance = 3 + 10 * np.random.rand(len(people)) performance=data_year[data_year.columns[
plt.rcdefaults()
fig, ax = plt.subplots(figsize=(10,6))
# Example data
people = data_year[data_year.columns[1]]
y_pos = np.arange(len(people))
#performance = 3 + 10 * np.random.rand(len(people))
performance=data_year[data_year.columns[2]]
error = np.random.rand(len(people))
rects=ax.barh(y_pos, performance, xerr=error, align='center')
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('population (thousands)' , fontsize=15)
ax.text(0.9, 0.1, year,verticalalignment='bottom', horizontalalignment='right',transform=ax.transAxes,color='red',size=30)
for i, (p, pr) in enumerate(zip(data_year["Country or area"], data_year["Urban Agglomeration"])):
plt.text(s=p, x=1, y=i, color='black', verticalalignment="top", horizontalalignment="left", size=8)
plt.text(s=pr , x=1, y=i, color='black',verticalalignment="bottom",horizontalalignment="left", size=10)
display(plt.show)
结果是这样的
但我需要的结果是这样的
如何更改标签的位置
使用.patches提取的矩形尺寸设置x位置,并使用ha='right'。
也看到这个。
2015年和所有国家的数据
从下载的文件中选择数据
从链接下载数据文件,或使用答案底部的示例数据框df。
作为pd进口熊猫
将matplotlib.pyplot作为plt导入
加载文件
数据=pd.read_csv'data/UNdata_Export_20210131_205829459.csv'
过滤数据
data=data.loc[data['City type']=='City property'和data['Sex']=='Both Sex'][['Country或Area','City','Value','Year']]
排序数据
data=data.sort_值'Value',升序=False
选择前10行
df=data.iloc[:10]
用OP法绘图
此实现不必要地将数据帧列提取到新变量中,例如人员
people=df[df.columns[2]]
y_pos=np.Arangelen人
错误=np.random.randlenpeople
图,ax=plt.子图尺寸=10,6
rects=ax.barhy\u pos,people,xerr=error,align='center'
ax.invert_yaxis标签从上到下读取
ax.set_xlabel'population',fontsize=15
ax.text0.9,0.12015,va='bottom',ha='right',transform=ax.transAxes,color='red',size=30
对于PDF[“国家或地区”]、df[“城市”]中的i、p、pr:
从ax中提取矩形
矩形=最大补片[i]
拿酒吧的x
x=矩形。获取
获得宽度
宽度=矩形。获取宽度
计算标签位置
x_标签=x+宽度
使用矩形x打印文本,并将ha设置为右侧而不是左侧
plt.text=p,x=x_标签,y=i,颜色='黑色',va=top,ha=right,尺寸=8,重量='bold'
plt.text=pr,x=x_标签,y=i,颜色='黑色',va=bottom,ha=right,尺寸=10,重量='bold'
displayplt.show
简化实施
绘制特定的dataframe列,而不将其提取到新变量
使用代替
绘制数据帧
ax=df.plot.barhy='Value',figsize=10,6,legend=False,width=0.8
ax.set_xlabel'population',fontsize=15
ax.setyticklabels=[]删除y记号标签
ax.tick_paramsleft=False删除刻度
ax.invert_yaxis
ax.text0.9,0.12015,va='bottom',ha='right',transform=ax.transAxes,color='red',size=30
对于PDF[“国家或地区”]、df[“城市”]中的i、p、pr:
从ax中提取矩形
矩形=最大补片[i]
拿酒吧的x
x=矩形。获取
获得宽度
宽度=矩形。获取宽度
计算标签位置
x_标签=x+宽度
使用矩形x打印文本,并将ha设置为右侧而不是左侧
plt.text=p,x=x_标签,y=i,颜色='黑色',va=top,ha=right,尺寸=8,重量='bold'
plt.text=pr,x=x_标签,y=i,颜色='黑色',va=bottom,ha=right,尺寸=10,重量='bold'
节目
绘图结果
测向样本
如果网站数据不再可用,则测试数据
数据=“国家或地区”:[韩国共和国]、“墨西哥”、“美利坚合众国”、“中国、香港特区”、“新加坡”、“智利”、“澳大利亚”、“澳大利亚”、“约旦”、“城市”:[墨西哥'、'东京'、'墨西哥'、'德'、'纽约纽约'、'香港SAR '、'新加坡'、'圣地亚哥'、'悉尼'、'墨尔本'、'墨尔本'、'值':[9860372.09272740.0、8854560.0、8550405.0、7291300.0、5535002.0、5150010.0、4526479.0、4353514.0、4007526.0],“年”:[2015年、2015年、2015年、2015年、2015年、2015年、2015年、2015年]]
df=pd.DataFramedata
displaydf
国家或地区城市价值年
0大韩民国首尔9860372.0 2015
1日本东京9272740.0 2015
2 2015年墨西哥城8854560.0
3美利坚合众国纽约州纽约市2015年第8550405.0号
4中国、香港特别行政区香港特别行政区7291300
5新加坡5535002.0 2015
6智利圣地亚哥5150010.0 2015
7澳大利亚悉尼4526479.0 2015
8澳大利亚墨尔本4353514.0 2015
9约旦安曼4007526.0 2015
希望答案是有帮助的。彻底回答问题是很费时的。如果你的问题已经解决,请接受解决方案✔ 低于▲/▼ 箭头,位于答案的左上角。如果出现更好的解决方案,则可以接受新的解决方案。您还可以对答案的有用性进行投票,并使用▲/▼ 如果解决方案不能回答问题,请留下评论。谢谢。