3

I'm using matplotlib, and I want to compare the graphs of 2 datasets by keeping the x and y axis same for both datasets. However, autoscale keeps butting in and rescaling my graphs a bit, because dataset 2 has smaller limit. As seen in pictures.

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()

Dataset 1

enter image description here

Dataset 2

enter image description here

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
M_D
  • 332
  • 1
  • 4
  • 11
  • @tacaswell: I know the 2 questions sound similar, but I tried set_xlim() and it didn't produce the result I want. – M_D Dec 28 '16 at 23:53
  • ok. question re-opened. – tacaswell Dec 28 '16 at 23:54
  • actually now I fixed it. I need to put ax = plt.subplot() and ax.set_xlim() and ax.set_ylim() **inside** the loop. – M_D Dec 29 '16 at 00:00
  • well, now I can't re-close it! I suggest answering your own question. You might also do better wit `fig, ax_array = plt.subplots(4, 4, sharex='all', sharey='all')` and looping over the axes object in `ax_array` (which is a 4x4 numpy array). – tacaswell Dec 29 '16 at 00:02

1 Answers1

0

You can user the xlim and ylim functions to set the limits of each axis. If the limits are constant, for example:

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

A more general solution would be to determine your max limit based on all datasets you plan to plot beforehand, and set the limits likewise.

Wes Doyle
  • 2,199
  • 3
  • 17
  • 32
  • I tried it actually. I added ax1.set_xlim([0,8000]) ax1.set_ylim([0,45000]) – M_D Dec 28 '16 at 23:49
  • but result is the same – M_D Dec 28 '16 at 23:50
  • actually now I fixed it. I need to put ax = plt.subplot() and ax.set_xlim() and ax.set_ylim() inside the loop. – M_D Dec 29 '16 at 00:00
  • Aha, I see. I was just now trying to understand whether this was inside for loop. Also, sorry I did not mention set_xlim() and set_ylim() for subplots. – Wes Doyle Dec 29 '16 at 00:05