【问题标题】:overlapping subplots for matplotlibmatplotlib 的重叠子图
【发布时间】:2018-10-03 11:25:43
【问题描述】:

我正在尝试在 matplotlib 中获得 5 行和 2 列的子图。 变量 X 表示字典。 [*X] 提供字典中可用的键。 每个键都应该出现在不同的行中。

idx = 0
axes = []
for key, val in X.items():
    axes.append(plt.subplot(len([*X]),1,idx+1))
    axes[idx].scatter(X[key], Y[key], color='r')
    axes[idx].set_title(key)
    axes[idx].set_xlabel(title)
    axes[idx].set_ylabel('QoS')#, color='g')
    axes[idx].spines['right'].set_visible(False)
    axes[idx].spines['top'].set_visible(False)
    axes[idx].xaxis.set_ticks_position('bottom')
    axes[idx].yaxis.set_ticks_position('left')
    axes[idx].set_ylim([0,1])
    axes.append(plt.subplot(len([*X]),2,idx+1))
    tmp = idx+1
    axes[tmp].scatter(X[key], Y1[key], color='r')
    axes[tmp].set_title(key)
    axes[tmp].set_xlabel(title)
    axes[tmp].set_ylabel('Power', color='g')
    axes[tmp].spines['right'].set_visible(False)
    axes[tmp].spines['top'].set_visible(False)
    axes[tmp].xaxis.set_ticks_position('bottom')
    axes[tmp].yaxis.set_ticks_position('left')
    #axes[idx].set_ylim([0,1])
    idx+=1

直觉上,我认为应该是这样,假设len([*X])是5。

 511, 521
 512, 522
 513, 523
 513, 523
 514, 525

使用当前设置,我是这样理解的。 我错过了什么?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    这似乎有点令人费解。要了解如何定义子图,请参阅 In Matplotlib, what does the argument mean in fig.add_subplot(111)?

    但是,这里可能不单独定义每个子图,而是一次创建它们。

    fig, axes = plt.subplots(nrows=5, ncols=2)
    for (key, val), axrow in zip(X.items(), axes):
        axrow[0].scatter(X[key], Y[key])
        axrow[1].scatter(X[key], Y1[key])
    

    【讨论】:

      猜你喜欢
      • 2021-10-12
      • 2013-01-08
      • 2013-03-24
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 2015-12-25
      相关资源
      最近更新 更多