Matplotlib sharex无法按预期工作 [英] Matplotlib sharex not working as expected

查看:75
本文介绍了Matplotlib sharex无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定我遗漏了一些明显的东西,但为什么 sharex=True, sharey=True 不起作用?我希望两个子图的 xlims 和 xticks 相同,并且两个子图的 ylims/yticks 相同.使用Python 3.8.3,matplotlib 3.2.1.

I'm sure I am missing something obvious, but why is sharex=True, sharey=True not working? I expected the xlims and xticks to be the same for both subplots, and the ylims/yticks to be the same for both subplots. Using Python 3.8.3, matplotlib 3.2.1.

x1, y1 = [(2,7,1), (6,2,2)]
x2, y2 = [(8,3,0), (1,4,9)]

fig, ax = plt.subplots(2,1, sharex=True, sharey=True, figsize=(15, 8));
ax1 = plt.subplot(2,1,1);
ax1.scatter(x1, y1,  c='red', label='Set1');
ax2 = plt.subplot(2,1,2);
ax2.scatter(x2, y2, c='black', label='Set2');

推荐答案

ax1 = plt.subplot(2,1,1); 创建一个与 ax .您想要:

ax1 = plt.subplot(2,1,1); creates a different (new) subplot than those already in ax. You want:

fig, ax = plt.subplots(2,1, sharex=True, sharey=True, figsize=(15, 8));
ax1, ax2 = ax

ax1.scatter(x1, y1,  c='red', label='Set1');
ax2.scatter(x2, y2, c='black', label='Set2');

或者干脆:

fig, ax = plt.subplots(2,1, sharex=True, sharey=True, figsize=(15, 8));


ax[0].scatter(x1, y1,  c='red', label='Set1');
ax[1].scatter(x2, y2, c='black', label='Set2');

输出:

这篇关于Matplotlib sharex无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆