I'm wondering how do I force my subplots to share the y-axis range. This is my code:
f, axes = plt.subplots(7, 1, sharex='col', sharey='row', figsize=(15, 30))
distance = []
for i in range(simulations):
delta = numpy.zeros((simulations+samples, simulations+samples))
data_x = sample_x[i*samples:(i*samples)+samples] + ensamble_x
data_y = sample_y[i*samples:(i*samples)+samples] + ensamble_y
for j in range(simulations+samples):
for k in range(simulations+samples):
if j <= k:
dist = similarity_measure((data_x[j].flatten(), data_y[j].flatten()), (data_x[k].flatten(), data_y[k].flatten()))
delta[j, k] = delta[k, j] = dist
delta = 1-((delta+1)/2)
delta /= numpy.max(delta)
model = manifold.TSNE(n_components=2, random_state=0, metric='precomputed')
coords = model.fit_transform(delta)
mds = manifold.MDS(n_components=2, max_iter=3000, eps=1e-9, random_state=0,
dissimilarity="precomputed", n_jobs=1)
coords = mds.fit(delta).embedding_
close, far = find_distance(coords[:samples, :], coords[samples+i, :])
distance.append((close, far))
axes[i].scatter(coords[:samples, 0], coords[:samples, 1], marker='x', c=colors[i], s=50, edgecolor='None')
axes[i].scatter(coords[samples:, 0], coords[samples:, 1], marker='o', c=colors, s=50, edgecolor='None')
axes[i].scatter(coords[close, 0], coords[close, 1], marker='s', facecolor="none", c=colors[i], s=50, edgecolor='None')
axes[i].scatter(coords[far, 0] , coords[far, 1] , marker='s', facecolor="none", c=colors[i], s=50, edgecolor='None')
axes[i].set_title('Simulation '+str(i+1), fontsize=20)
markers = []
labels = [str(n+1) for n in range(simulations)]
for i in range(simulations):
markers.append(Line2D([0], [0], linestyle='None', marker="o", markersize=10, markeredgecolor="none", markerfacecolor=colors[i]))
lgd = plt.legend(markers, labels, numpoints=1, bbox_to_anchor=(1.0, -0.055), ncol=simulations)
plt.tight_layout()
plt.ylim(-1, 1)
plt.axis('equal')
plt.savefig('Simulations.pdf', bbox_extra_artists=(lgd,), format='pdf', bbox_inches='tight')
And it's result:
As can be seen, the y axis limits differs from one subplot to another. I'd like to use the max/min range generated.
Thank you.
EDTI: MINIMAL EXAMPLE
%matplotlib inline
from sklearn.preprocessing import normalize
from sklearn import manifold
from matplotlib import pyplot as plt
from matplotlib.lines import Line2D
import numpy
import itertools
f, axes = plt.subplots(7, 1, sharex='col', sharey='row', figsize=(15, 30))
distance = []
for i in range(7):
delta = numpy.random.randint(0, 100, (100, 100))
axes[i].scatter(delta[:, 0], delta[:, 1], marker='x', c='r', s=50, edgecolor='None')
axes[i].set_title('Simulation '+str(i+1), fontsize=20)
axes[i].set_ylim(0, 100)
markers = []
plt.tight_layout()
plt.axis('equal')
You have to add a line axes[i].set_ylim(ymin,ymax) within the main loop where you make the plot. For example, below the following line
add:
That should solve it.
In your example, you are calling plt.ylim instead, but from the documentation "Get or set the y-limits of the current axes", which in your case correspond to the last axes.
Answer to the minimalist example:
As you see from your plot, all the axis but the last, have the same limits in the y-coordinate. Everytime you call plt.*, you affect the behaviour of the last axis. Your last call to plt.axis('equal') is what affects the last plot. Just remove this line.
Your 1st line
has an inappropriate value for the
sharey
parameter. Usingsharey='row'
you're asking that all the subplots in each row share the same y axis, but each of your subplots is in a row by itself, so you see no sharing.If you try
sharey=True
orsharey='col'
you'll get what you want.Addendum
The following code
gives me the following two plots. Can you spot a single difference?