Python Matplotlib关闭自动

2024-05-16 13:57:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是matplotlib,我想通过保持两个数据集的x轴和y轴相同来比较两个数据集的图形。然而,autoscale不断地插接并重新调整我的图,因为数据集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

enter image description here

数据集2

enter image description here


Tags: 数据dataparampltgreenfigureautoscalecustomers
1条回答
网友
1楼 · 发布于 2024-05-16 13:57:15

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

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

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

相关问题 更多 >