我使用MatPultLIB,我想通过保持X和Y轴对于两个数据集都相同来比较2个数据集的图表。但是,autoscale会不断地插入并重新调整我的图的大小,因为dataset 2有更小的限制。如图所示。

def make_figure(data, param ='Customers'):  # default param is Customers
    fig = plt.figure(figsize = (18,10))

    xticks = np.arange(0, 10000, 1000)
    yticks = np.arange(0, 55000, 5000)

    i = 0
    colors = ['red','yellow','brown','orange','green','green','green','green','blue','cyan','navy','magenta']

    ax1 = fig.add_subplot(3,4,1)
    ax1.set_xticks(xticks)
    ax1.set_yticks(yticks)
    ax1.autoscale(False, tight=False)

    for assortment in ['a','b','c']:
        for storetype in ['a','b','c','d']:

            datax = data[param][data.StoreType == storetype][data.Assortment == assortment]
            datay = data['Sales'][data.StoreType == storetype][data.Assortment == assortment]
            plt.subplot(3, 4, i+1, sharex=ax1, sharey=ax1)
            plt.title('Assortment ' + assortment + ' StoreType ' + storetype)
            plt.scatter(y = datay, x = datax, c=colors[i], alpha=.65)

            if i % 4 == 0:
                plt.ylabel('Sales')

            if i >= 8:
                plt.xlabel(str(param))

            i += 1

    plt.tight_layout()

    return plt.show()

数据集1
python - Python Matplotlib-关闭自动缩放-LMLPHP
数据集2
python - Python Matplotlib-关闭自动缩放-LMLPHP

最佳答案

您可以使用XLIM和YLIM函数来设置每个轴的限制。
如果限制是恒定的,例如:

# Set the limits and disable scaling
plt.xlim(0, 8000)
plt.ylim(0, 40000)
plt.autoscale(False)

一个更普遍的解决方案是,根据计划预先绘制的所有数据集确定最大限制,并同样设置限制。

关于python - Python Matplotlib-关闭自动缩放,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41354224/

10-09 08:43