相关疑难解决方法(0)

matplotlib barplot,在条形图中间设置xticklabels的通用方法

以下代码生成一个条形图,其中xticklabels以每个条形为中心.但是,缩放x轴,更改条数或更改条宽会改变标签的位置.是否存在处理该行为的通用方法?

# This code is a hackish way of setting the proper position by trial
# and error.
import matplotlib.pyplot as plt
import numpy as np
y = [1,2,3,4,5]
# adding 0.75 did the trick but only if I add a blank position to `xl`
x = np.arange(0,len(y)) + 0.75
xl = ['', 'apple', 'orange', 'pear', 'mango', 'peach']

fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(x,y,0.5)
ax.set_xticklabels(xl)
# I cannot change the scaling without changing the position of the tick labels …
Run Code Online (Sandbox Code Playgroud)

python matplotlib

3
推荐指数
1
解决办法
2505
查看次数

标签 统计

matplotlib ×1

python ×1