【问题标题】:Matplotlib sharex not working as expectedMatplotlib sharex 未按预期工作
【发布时间】:2020-11-24 20:44:54
【问题描述】:

我确定我遗漏了一些明显的东西,但为什么 sharex=True, sharey=True 不起作用?我希望两个子图的 xlims 和 xticks 相同,并且两个子图的 ylims/yticks 相同。使用 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');

【问题讨论】:

    标签: python matplotlib subplot axes


    【解决方案1】:

    ax1 = plt.subplot(2,1,1); 创建一个与ax 中已有的子图不同的(新)子图。你想要:

    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');
    

    输出:

    【讨论】:

      猜你喜欢
      • 2021-10-19
      • 2016-02-19
      • 1970-01-01
      • 2021-06-04
      • 2022-01-24
      • 2015-05-11
      • 2020-05-15
      • 2014-10-31
      • 2018-02-12
      相关资源
      最近更新 更多