Setting equal margins while keeping range of ticks as defined

Viewed 47

The problem I have so far is that I have two plots in which, even though I specify the ticks ranges, as well as tried specifying the margins, I am still not able to get both plots to be. The problem seems to be the margins are not the same, however, when trying to fix by setting a common y margin, the result is not as the expected (second pic), it seems to ignore the tick frequency that I set. My goal is just to get both plots on the same format as I am putting them side-by-side

enter image description here

enter image description here

Here is my code:

plt.figure()
plt.scatter(x = x1, y = y1, marker = '.', s = 1.2, alpha = 0.75, label = 'DATA', color = 'darkorange')
plt.yticks(np.arange(0,1.2,0.2))
plt.xticks(np.arange(0,420,60))
lgnd = plt.legend(loc=1, scatterpoints=1, fontsize=10)
lgnd.legendHandles[0]._sizes = [30]
# plt.margins(y = 0.02)
plt.show()

plt.figure()
plt.scatter(x = x2, y = y2, marker = '.', s = 1.2, alpha = 0.75, label = 'DATA')
plt.xticks(np.arange(0,420,60))
plt.yticks(np.arange(0,1.2,0.2))
lgnd = plt.legend(loc=1, scatterpoints=1, fontsize=10)
lgnd.legendHandles[0]._sizes = [30]
# plt.margins(y = 0.02)
plt.show()

2 Answers

You can use plt.ylim(ymin, ymax) and plt.xlim(xmin, xmax) to set the same plot size. Here is a code that works:

# Get the max value in both y axis. You could also to this for the x axis
# as well as get the minimun value. Here I'm just assuming it's zero
y1max = max(y1)
y2max = max(y2)
ymax = max(y1max, y2max)

plt.figure()
plt.scatter(x = x1, y = y1, marker = '.', s = 1.2, alpha = 0.75, label = 'DATA', color = 'darkorange')
plt.yticks(np.arange(0,1.2,0.2))
plt.xticks(np.arange(0,420,60))

# After you create the figure, set the y max and min values
plt.ylim(0, ymax)

lgnd = plt.legend(loc=1, scatterpoints=1, fontsize=10)
lgnd.legendHandles[0]._sizes = [30]
plt.show()

plt.figure()
plt.scatter(x = x2, y = y2, marker = '.', s = 1.2, alpha = 0.75, label = 'DATA')
plt.xticks(np.arange(0,420,60))
plt.yticks(np.arange(0,1.2,0.2))

# Do the same for the second plot, with the same ymax value
plt.ylim(0, ymax)

lgnd = plt.legend(loc=1, scatterpoints=1, fontsize=10)
lgnd.legendHandles[0]._sizes = [30]
plt.show()

Here is what i got. My data is not in the same range as yours, so the ticks don't go until the end, but the y axis scale is the same, as you can see.

Both plots side by side

If I understand your question correctly, you are looking for plt.ylim and plt.xlim, which allows to setup the minimum and maximum of axis. Additionally, you can include figsize option within plt.figure() to ensure that both of the generated figures are of equal sizes. Here is an example based on your code:

import matplotlib.pyplot as plt
import numpy as np

#generating test data
x1 = np.random.random((300,1))
x2 = np.random.random((300,1))
y1 = np.random.random((300,1))
y2 = np.random.random((300,1))

#setting the size of the figure
plt.figure(figsize=(8,6))
plt.scatter(x = x1, y = y1, marker = 'o', s = 3.2, alpha = 0.75, label = 'DATA', color = 'darkorange')
#plt.yticks(np.arange(0,1.2,0.2))
#plt.xticks(np.arange(0,420,60))
lgnd = plt.legend(loc=1, scatterpoints=1, fontsize=10)
lgnd.legendHandles[0]._sizes = [30]
# plt.margins(y = 0.02)

#setting minimum and maximum of the plots
plt.ylim([0,1])
plt.xlim([0,1])
plt.show()

#setting the size of the figure
plt.figure(figsize=(8,6))
plt.scatter(x = x2, y = y2, marker = 'o', s = 3.2, alpha = 0.75, label = 'DATA')
#plt.xticks(np.arange(0,420,60))
#plt.yticks(np.arange(0,1.2,0.2))

#setting minimum and maximum of the plots
plt.ylim([0,1])
plt.xlim([0,1])
lgnd = plt.legend(loc=1, scatterpoints=1, fontsize=10)
lgnd.legendHandles[0]._sizes = [30]
# plt.margins(y = 0.02)
plt.show() 

Output:

enter image description here

enter image description here

Side by Side:

enter image description here

Related