小编Con*_*nti的帖子

matplotlib中的两个条形图重叠错误的方式

我正在使用Python中的matplotlib创建一个条形图,我对重叠条有一点问题:

import numpy as np
import matplotlib.pyplot as plt

a = range(1,10)
b = range(4,13)
ind = np.arange(len(a))
width = 0.65

fig = plt.figure()
ax = fig.add_subplot(111)

ax.bar(ind+width, a, width, color='#b0c4de')

ax2 = ax.twinx()
ax2.bar(ind+width+0.35, b, 0.45, color='#deb0b0')

ax.set_xticks(ind+width+(width/2))
ax.set_xticklabels(a)

plt.tight_layout()
Run Code Online (Sandbox Code Playgroud)

Barplot

我希望蓝色条纹在前面,而不是红色条纹.到目前为止,我设法做到的唯一方法是切换ax和ax2,但是ylabels也将被反转,这是我不想要的.是不是有一种简单的方法告诉matplotlib在ax之前渲染ax2?

此外,右侧的ylabels被plt.tight_layout()切断.有没有办法在仍然使用tight_layout时避免这种情况?

python matplotlib bar-chart overlapping

5
推荐指数
1
解决办法
5273
查看次数

标签 统计

bar-chart ×1

matplotlib ×1

overlapping ×1

python ×1