python - Matplotlib twinx 与 yticklabels 和 yticks 对齐

标签 python matplotlib

还有其他关于它的问题,但似乎没有一个问题涵盖了我的确切问题/他们的解决方案都没有“纠正”我的问题。

import matplotlib.pyplot as plt
calendar = [[1,0,0,1,1,0,1],[0,0,1,1,1,0,1]]
fig, ax = plt.subplots()
namelist = ['alpha','beta']
yticks = [n for n in range(len(namelist))]
ax.set_yticks(yticks)
ax.set_yticklabels(namelist)
ax2 = ax.twinx()
ax2.set_yticks(ax.get_yticks())
ax2.set_yticklabels(['gamma','delta'])    

im = ax.imshow(calendar, interpolation='none', cmap='summer')
plt.show()

enter image description here

当然,上面只是一个非常简化的版本,但它应该足以看到问题:当注释掉两条“ax2线”时,绘图是我想要的,但是当添加第二个y轴时,绘图的大小调整如下我不想要它。我希望 alpha/beta 与 gamma/delta 处于相同的高度(这样就不会剩下空白),但我现在失败了很长一段时间。我已经尝试过 get_yticks() 函数(如一些类似问题中所建议的那样),但没有帮助。 提前致谢!

编辑:一个小细节:该解决方案应该是“可扩展的”,从某种意义上说,它应该处理更多的 y 值(alpha、beta、delta...),并且它们的孪生对应物位于前面。

编辑2:(我不确定发布新问题是否有用,所以我只是在这里问:)我现在正在尝试添加网格。我再次尝试了很多,但似乎没有任何帮助,最多我可以得到一个 y 轴网格。检查以下代码(并对那些未注释的行尝试相同的操作!):<​​/p>

import matplotlib.pyplot as plt
calendar = [[1,0,0,1,1,0,1],[0,0,1,1,1,0,1]]
fig, ax = plt.subplots()
namelist = ['alpha','beta']
yticks = [n for n in range(len(namelist))]
ax.set_xticks([num for num in range(len(calendar[0]))])
ax.set_xticks([num-0.5 for num in range(len(calendar[0]))],minor=True)
ax.set_yticks(yticks)
ax.set_yticks([num-0.5 for num in range(len(namelist))],minor=True)
ax.set_yticklabels(namelist)
im = ax.imshow(calendar, interpolation='none', cmap='summer')
ax.set_adjustable('box-forced')
ax.xaxis.grid(which='major',alpha=1,color='k',lw=2)
ax.xaxis.grid(which='minor',alpha=1,color='k',lw=2)
ax.yaxis.grid(which='minor',alpha=1,color='k',lw=2)

#ax2 = ax.twinx()
#ax2.set_yticks(ax.get_yticks())
#ax2.set_yticklabels(['gamma','delta'])    
#im2 = ax2.imshow(calendar, interpolation='none', cmap='summer')
#ax2.set_adjustable('box-forced')

plt.show()

如何让网格显示出来?这是它的情况的图片(没有 ax2)。基本上,代码不应该给我完全相同的结果,但第二个标签在右边吗?! enter image description here

最佳答案

获得所需内容的一种方法是在 ax2 上重新绘制:

im2 = ax2.imshow(calendar, interpolation='none', cmap='summer')

它并不完美,但它有效。

ticks in correct positions

编辑:

按照答案here ,如果您进一步在轴上使用 set_adjustable,空白就会消失:

import matplotlib.pyplot as plt
calendar = [[1,0,0,1,1,0,1],[0,0,1,1,1,0,1]]
fig, ax = plt.subplots()
namelist = ['alpha','beta']
yticks = [n for n in range(len(namelist))]
ax.set_yticks(yticks)
ax.set_yticklabels(namelist)
im = ax.imshow(calendar, interpolation='none', cmap='summer')
ax.set_adjustable('box-forced')

ax2 = ax.twinx()
ax2.set_yticks(ax.get_yticks())
ax2.set_yticklabels(['gamma','delta'])    
im2 = ax2.imshow(calendar, interpolation='none', cmap='summer')
ax2.set_adjustable('box-forced')

plt.show()

whitespace removed

关于python - Matplotlib twinx 与 yticklabels 和 yticks 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45445803/

相关文章:

python - python中的手动直方图

python - matplotlib中控制散点图y轴顺序

python - docker-compose exec web python manage.py makemigrations问题

php - 如何使用PHP调用python脚本实时返回结果?

python - 比较二维 numpy 数组的元素

python - 与从离散数据插值的曲线相切

python - 我可以将 3 个大小相同的子图排列成三角形吗?

python - 使用 Python 注入(inject)原始 TCP 数据包

java - Jython,只使用来自 Java 的 Python 的方法?

python - 将错误栏添加到图例的 Line2D 元素中的标记