我用这段代码绘制了一些数据:
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'
。从下载的文件中选择数据
df
李>用OP法绘图
people
)简化实施
绘图结果
df
样本相关问题 更多 >
编程相关推荐