简体   繁体   English

如何在matplotlib中将条形图的xticks居中?

[英]How to center the xticks from a bar chart in matplotlib?

I was following this tutorial to add value in my bar chart link我正在按照本教程在我的条形图链接中添加价值

The code that shown in the blog written like this博客中显示的代码是这样写的

from matplotlib import pyplot as plt
import numpy as np

years = [1901, 1911, 1921, 1931, 1941, 1951, 1961, 1971, 1981, 1991, 2001, 2011]
population = [237.4, 238.4, 252.09, 251.31, 278.98, 318.66, 361.09, 439.23, 548.16, 683.33, 846.42, 1028.74]

x = np.arange(len(years)) # the label locations
width = 0.35 # the width of the bars

fig, ax = plt.subplots()

ax.set_ylabel('Population(in million)')
ax.set_title('Years')
ax.set_xticks(x)
ax.set_xticklabels(years)

pps = ax.bar(x - width/2, population, width, label='population')
for p in pps:
   height = p.get_height()
   ax.annotate('{}'.format(height),
      xy=(p.get_x() + p.get_width() / 2, height),
      xytext=(0, 3), # 3 points vertical offset
      textcoords="offset points",
      ha='center', va='bottom')

plt.show()

So, the output become like this所以,输出变成这样

在此处输入图片说明

But, I just realized that the xticks from the bar chart is not centered但是,我刚刚意识到条形图中的 xticks 没有居中

在此处输入图片说明

My question - Is anybody know how to center the xticks?我的问题 - 有人知道如何将 xticks 居中吗?

I was trying to find the solution, but not found yet我试图找到解决方案,但还没有找到

Any help would be appreciated, thanks任何帮助将不胜感激,谢谢

In matplotlib 3.4.0 or newer bar_label can be used instead of annotate :matplotlib 3.4.0 或更新bar_label中,可以使用bar_label代替annotate

import numpy as np
from matplotlib import pyplot as plt

years = [1901, 1911, 1921, 1931, 1941, 1951, 1961, 1971, 1981, 1991, 2001, 2011]
population = [237.4, 238.4, 252.09, 251.31, 278.98, 318.66, 361.09, 439.23,
              548.16, 683.33, 846.42, 1028.74]

# Create Tick Locations
x = np.arange(len(years))
# Plotting
fig, ax = plt.subplots(figsize=(10, 6))
# Do not offset the values of x to center labels (default)
ax.bar(x, population, width=.35, label='population')

# Labels, ticks, etc.
ax.set_ylabel('Population(in million)')
ax.set_title('Years')
ax.set_xticks(x)
ax.set_xticklabels(years)

# Add Bar Labels
for c in ax.containers:
    ax.bar_label(c)

plt.show()

阴谋

imo that tutorial is making plotting more confusing than it needs to be. imo 该教程使绘图变得比需要的更加混乱。 You seldom need to fetch coordinates from the artists on the plot.您很少需要从绘图上的艺术家那里获取坐标。 Just use your data directly:只需直接使用您的数据:

from matplotlib import pyplot as plt
import numpy as np

years = [1901, 1911, 1921, 1931, 1941, 1951, 1961, 1971, 1981, 1991, 2001, 2011]
population = [237.4, 238.4, 252.09, 251.31, 278.98, 318.66, 361.09, 439.23, 548.16, 683.33, 846.42, 1028.74]

x_values = np.arange(len(years))
fig, ax = plt.subplots(figsize=(12, 4))

ax.bar(x_values, population, width=.35, label='population')
for x, y in zip(x_values, population):    
    ax.annotate(
        str(y),        # label is our y-value as a string
        xy=(x, y),
        xytext=(0, 3), # 3 points vertical offset
        textcoords="offset points",
        ha='center', 
        va='bottom'
    )
    
    
ax.set_xticks(x_values)
ax.set_xticklabels(years)
ax.set_ylim(0, 1200)

plt.show()

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM