Make a frame with subplots in Matplotlib - python

I want to make a figure which consist of a frame with 4 figures, but in each figure there are three subplots. I am using the current version of Matplotlib
I show my code in order to do each individual figure, the point as I comment before, is how put 4 of this plots together in order to make a single figure
filename1 = "file1.txt"
filename2 = "file2.txt"
filename3 = "file3.txt"
datalist1 = np.loadtxt(filename1)
datalist2 = np.loadtxt(filename2)
datalist3 = np.loadtxt(filename3)
f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True)
#First subplot
ax1.plot(datalist1[:,0], datalist1[:,1], 'k-')
ax1.plot(datalist2[:,0], datalist2[:,1], 'b-')
ax1.plot(datalist2[:,0], datalist2[:,2], 'g-')
ax1.plot(datalist2[:,0], datalist2[:,3], 'r-')
ax1.plot(datalist3[:,0], datalist3[:,1], 'k--')
ax1.set_ylim(-1.2, 1.2)
ax1.set_xlim(0, 10)
major_ticks_x = np.arange(0.0, 11, 2.0)
minor_ticks_x = np.arange(0.0, 11, 1.0)
major_ticks_y = np.arange(-1, 1.05, 1.0)
minor_ticks_y = np.arange(-1, 1.05, 0.25)
ax1.set_yticks(major_ticks_y)
ax1.set_yticks(minor_ticks_y, minor=True)
#Second subplot
ax2.plot(datalist1[:,0], datalist1[:,2], 'k-')
ax2.plot(datalist2[:,0], datalist2[:,4], 'b-')
ax2.plot(datalist2[:,0], datalist2[:,5], 'g-')
ax2.plot(datalist2[:,0], datalist2[:,6], 'r-')
ax2.plot(datalist3[:,0], datalist3[:,1], 'k--')
ax2.set_ylim(-1.2, 1.2)
ax2.set_xlim(0, 10)
ax2.set_yticks(major_ticks_y)
ax2.set_yticks(minor_ticks_y, minor=True)
#Third subplot
ax3.plot(datalist1[:,0], datalist1[:,3], 'k-')
ax3.plot(datalist2[:,0], datalist2[:,7], 'b-')
ax3.plot(datalist2[:,0], datalist2[:,8], 'g-')
ax3.plot(datalist2[:,0], datalist2[:,9], 'r-')
ax3.plot(datalist3[:,0], datalist3[:,1], 'k--')
ax3.set_ylim(-1.2, 1.2)
ax3.set_xlim(0, 10)
ax3.set_yticks(major_ticks_y)
ax3.set_yticks(minor_ticks_y, minor=True)
ax3.set_xticks(major_ticks_x)
ax3.set_xticks(minor_ticks_x, minor=True)
ax3.set_xlabel(r"$t$")
f.subplots_adjust(hspace=0.0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
The plot that I want to obtain is somtehing like this, in a single figure:
Somebody knows how can be do it?? Thanks for your attention.

OK, I'll bite. It is unclear what you want, but I assume you want 12 subplots (6 rows, 2 columns) grouped into 4 groups with shared x-axis.
As usual creating the subplots and plotting is easy. Sharing x-axis is straightforward as well, but requires some manual work. You can either set up the shared x-axis during the subplot creation or modify it after. I think modifying after is simpler.
Sorry for the manual part in the middle - it is possible to automate obviously.
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
fig, axx = plt.subplots(6, 2, figsize=(10,14))
## merge axis
axx[0, 0].get_shared_x_axes().join(axx[0, 0], axx[2, 0])
axx[0, 0].set_xticklabels([])
axx[1, 0].get_shared_x_axes().join(axx[1, 0], axx[2, 0])
axx[1, 0].set_xticklabels([])
axx[0, 1].get_shared_x_axes().join(axx[0, 1], axx[2, 1])
axx[0, 1].set_xticklabels([])
axx[1, 1].get_shared_x_axes().join(axx[1, 1], axx[2, 1])
axx[1, 1].set_xticklabels([])
axx[3, 0].get_shared_x_axes().join(axx[3, 0], axx[5, 0])
axx[3, 0].set_xticklabels([])
axx[4, 0].get_shared_x_axes().join(axx[4, 0], axx[5, 0])
axx[4, 0].set_xticklabels([])
axx[3, 1].get_shared_x_axes().join(axx[3, 1], axx[5, 1])
axx[3, 1].set_xticklabels([])
axx[4, 1].get_shared_x_axes().join(axx[4, 1], axx[5, 1])
axx[4, 1].set_xticklabels([])
# plot some data
for i, row in enumerate(axx):
for j, cell in enumerate(row):
if i <= 2:
cell.plot(np.random.rand(100))
else:
cell.plot(np.random.rand(200))
Here is the result.

Related

Adding a stacked plot as a subplot in python

Please I need help with a plot. I am making a 3x3 dimension figure containing 7 subplots. I want two(2) of the subplots (ax6 and ax7) to be stacked plots. Does anyone have an idea how I can make this work? I used the code below to make the grid.
fig = plt.figure()
fig.set_figheight(8)
fig.set_figwidth(10)
gs = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(gs[0, 0])
ax2 = plt.subplot(gs[0, -2])
ax3 = plt.subplot(gs[0, -1])
ax4 = plt.subplot(gs[1, 0])
ax5 = plt.subplot(gs[-1, 0])
ax6 = plt.subplot(gs[1:, -2])
ax7 = plt.subplot(gs[1:, -1])
I tried making the stacked plot for ax6 using the code below
ax6[0].plot(s[['xa']], s[['ac1']], label = "Data")
ax6[0].plot(s[['xa']], s[['ac2']], label = "C-C")
ax6[0].plot(s[['xa']], s[['ac3']], label = "C-O")
ax6[0].plot(s[['xa']], s[['ac4']], label = "C=C")
ax6[0].plot(s[['xa']], s[['ea1']], label = "Envelope")
ax6[0].text(0.08, 0.70, 'C', ha='center', va='baseline', wrap=True, fontsize= 10, fontweight='bold', color='darkgreen', transform=ax6[0].transAxes)
ax6[1].plot(s[['xb']], s[['bc1']], label = "Data")
ax6[1].plot(s[['xb']], s[['bc2']], label = "C-C")
ax6[1].plot(s[['xb']], s[['bc3']], label = "C-O")
ax6[1].plot(s[['xb']], s[['bc4']], label = "C=C")
ax6[1].plot(s[['xb']], s[['be1']], label = "Envelope")
ax6[1].text(0.08, 0.70, 'm.C', ha='center', va='baseline', wrap=True, fontsize= 10, fontweight='bold', color='darkgreen', transform=ax6[1].transAxes)
Please look at the comments in the code:
import matplotlib.pyplot as plt
from matplotlib import gridspec
import numpy as np
fig = plt.figure(figsize=(10, 8))
g = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(g[0, 0])
ax2 = plt.subplot(g[0, 1])
ax3 = plt.subplot(g[0, 2])
ax4 = plt.subplot(g[1, 0])
ax5 = plt.subplot(g[2, 0])
# Create another grid
g2 = gridspec.GridSpec(3, 3)
g2.update(hspace=0.00)
# Generate data for three subplots in g2
x = np.linspace(0, 2 * np.pi, 400)
ya = np.sin(x)
yb = np.cos(x)
y7 = np.sin(x) ** 2
# Get three different Axes objects
ax6a = plt.subplot(g2[1, 1])
ax6b = plt.subplot(g2[2, 1], sharex=ax6a)
ax7 = plt.subplot(g2[1:, -1])
# Hide the xticklabels of top subplot in the shared plots
plt.setp(ax6a.get_xticklabels(), visible=False)
# Set xticks for lower subplots in the shared plots
ax6b.set_xticks(np.pi * np.array([0, 1/2, 1, 3/2, 2]))
# Try plotting
ax6a.plot(x, ya)
ax6b.plot(x, yb, 'g')
ax7.plot(x, y7, 'r')
plt.tight_layout()
plt.show()
This gives:
This answer was motivated by this answer and examples from older documentation of matplotlib.
If you want ax7 (red color subplot here) represented in to two separate subplots, either create a new Gridspec or use g depending on attributes you want to assign them e.g. in the code above:
# ax7 = plt.subplot(g2[1:, -1])
# ax7.plot(x, y7, 'r')
ax7a = plt.subplot(g[1, 2])
ax7b = plt.subplot(g[2, 2])
ax7a.plot(x, y7, 'r')
ax7b.plot(x, y7, 'r')
This gives:

Modifying saved plot with matplotlib

I am having a problem right now. I have run an extremely heavy simulation and, thus, generated a plot with matplotlib containing the results and saved it (as .jpg). However, there are some elemnts of the plot I would like to change, such as labels size and one vertical line. Is there a straighforward way to do this using matplotlib? I know I could have stored the data and now just replot changing the parameters (and, actually, I have done this), but I was wondering whether there is an easier way. Maybe something like:
fig, ax = plt.figure(path_to_figure)
ax.set_ylabel("Y_label")
...
You can refer to below example, which gives you more idea on how you can do this while plotting everything.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline
plt.rc('text', usetex=True)
def f(t):
return t ** 2
t1 = np.arange(0.0, 2.0, 0.1)
noise = np.random.randn(len(t1)) * 0.04
# x coordinates for the lines
xcoords = [0.1, 0.3, 0.5]
# colors for the lines
colors = ['r','k','b']
fig = plt.figure(figsize=(4, 3), dpi=200)
ax = fig.add_subplot(1, 1, 1)
plt.scatter(t1, f(t1 + noise), color = 'hotpink', label='Values obtained by experiment', edgecolors='k')
plt.plot(t1, f(t1), ls='solid', label='Theoretical expectation', color='b')
plt.title(r'This is latex title example $\mathbf{E = m \times c^2}$', fontsize='small')
for xc,c in zip(xcoords,colors):
plt.axvline(x=xc, label='line at x = {}'.format(xc), c=c)
plt.grid()
plt.legend(loc=0)
If you want to make all the fonts bold, you can also use below code to make everything bold:
font = {'weight' : 'bold',
'size' : 14 }
plt.rc('font', **font)
def f(t):
return t ** 2
t1 = np.arange(0.0, 2.0, 0.1)
noise = np.random.randn(len(t1)) * 0.04
# x coordinates for the lines
xcoords = [0.1, 0.3, 0.5]
# colors for the lines
colors = ['r','k','b']
fig = plt.figure(figsize=(4, 3), dpi=200)
ax = fig.add_subplot(1, 1, 1)
plt.scatter(t1, f(t1 + noise), color = 'hotpink', label='Values obtained by experiment', edgecolors='k')
plt.plot(t1, f(t1), ls='solid', label='Theoretical expectation', color='b')
plt.title(r'This is latex title example $\mathbf{E = m \times c^2}$', fontsize='small')
plt.xlabel("This is X-label.", fontsize=12)
plt.ylabel("This is Y-label.", fontsize=16)
for xc,c in zip(xcoords,colors):
plt.axvline(x=xc, label='line at x = {}'.format(xc), c=c)
plt.grid()
plt.legend(loc=(1.15,0.2))

How to plot a mean line on a distplot between 0 and the y value of the mean?

I have a distplot and I would like to plot a mean line that goes from 0 to the y value of the mean frequency. I want to do this, but have the line stop at when the distplot does. Why isn't there a simple parameter that does this? It would be very useful.
I have some code that gets me almost there:
plt.plot([x.mean(),x.mean()], [0, *what here?*])
This code plots a line just as I'd like except for my desired y-value. What would the correct math be to get the y max to stop at the frequency of the mean in the distplot? An example of one of my distplots is below using 0.6 as the y-max. It would be awesome if there was some math to make it stop at the y-value of the mean. I have tried dividing the mean by the count etc.
Update for the latest versions of matplotlib (3.3.4) and seaborn (0.11.1): the kdeplot with shade=True now doesn't create a line object anymore. To get the same outcome as before, setting shade=False will still create the line object. The curve can then be filled with ax.fill_between(). The code below is changed accordingly. (Use the revision history to see the older version.)
ax.lines[0] gets the curve of the kde, of which you can extract the x and y data.
np.interp then can find the height of the curve for a given x-value:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x = np.random.normal(np.tile(np.random.uniform(10, 30, 5), 50), 3)
ax = sns.kdeplot(x, shade=False, color='crimson')
kdeline = ax.lines[0]
mean = x.mean()
xs = kdeline.get_xdata()
ys = kdeline.get_ydata()
height = np.interp(mean, xs, ys)
ax.vlines(mean, 0, height, color='crimson', ls=':')
ax.fill_between(xs, 0, ys, facecolor='crimson', alpha=0.2)
plt.show()
The same approach can be extended to show the mean together with the standard deviation, or the median and the quartiles:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
x = np.random.normal(np.tile(np.random.uniform(10, 30, 5), 50), 3)
fig, axes = plt.subplots(ncols=2, figsize=(12, 4))
for ax in axes:
sns.kdeplot(x, shade=False, color='crimson', ax=ax)
kdeline = ax.lines[0]
xs = kdeline.get_xdata()
ys = kdeline.get_ydata()
if ax == axes[0]:
middle = x.mean()
sdev = x.std()
left = middle - sdev
right = middle + sdev
ax.set_title('Showing mean and sdev')
else:
left, middle, right = np.percentile(x, [25, 50, 75])
ax.set_title('Showing median and quartiles')
ax.vlines(middle, 0, np.interp(middle, xs, ys), color='crimson', ls=':')
ax.fill_between(xs, 0, ys, facecolor='crimson', alpha=0.2)
ax.fill_between(xs, 0, ys, where=(left <= xs) & (xs <= right), interpolate=True, facecolor='crimson', alpha=0.2)
# ax.set_ylim(ymin=0)
plt.show()
PS: for the mode of the kde:
mode_idx = np.argmax(ys)
ax.vlines(xs[mode_idx], 0, ys[mode_idx], color='lime', ls='--')
With plt.get_ylim() you can get the limits of the current plot: [bottom, top].
So, in your case, you can extract the actual limits and save them in ylim, then draw the line:
fig, ax = plt.subplots()
ylim = ax.get_ylim()
ax.plot([x.mean(),x.mean()], ax.get_ylim())
ax.set_ylim(ylim)
As ax.plot changes the ylims afterwards, you have to re-set them with ax.set_ylim as above.

Scatterplot in matplotlib with legend and randomized point order

I'm trying to build a scatterplot of a large amount of data from multiple classes in python/matplotlib. Unfortunately, it appears that I have to choose between having my data randomised and having legend labels. Is there a way I can have both (preferably without manually coding the labels?)
Minimum reproducible example:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
X = np.random.normal(0, 1, [5000, 2])
Y = np.random.normal(0.5, 1, [5000, 2])
data = np.concatenate([X,Y])
classes = np.concatenate([np.repeat('X', X.shape[0]),
np.repeat('Y', Y.shape[0])])
Plotting with randomized points:
plot_idx = np.random.permutation(data.shape[0])
colors = pd.factorize(classes)
fig, ax = plt.subplots()
ax.scatter(data[plot_idx, 0],
data[plot_idx, 1],
c=colors[plot_idx],
label=classes[plot_idx],
alpha=0.4)
plt.legend()
plt.show()
This gives me the wrong legend.
Plotting with the correct legend:
from matplotlib import cm
unique_classes = np.unique(classes)
colors = cm.Set1(np.linspace(0, 1, len(unique_classes)))
for i, class in enumerate(unique_classes):
ax.scatter(data[classes == class, 0],
data[classes == class, 1],
c=colors[i],
label=class,
alpha=0.4)
plt.legend()
plt.show()
But now the points are not randomized and the resulting plot is not representative of the data.
I'm looking for something that would give me a result like I get as follows in R:
library(ggplot2)
X <- matrix(rnorm(10000, 0, 1), ncol=2)
Y <- matrix(rnorm(10000, 0.5, 1), ncol=2)
data <- as.data.frame(rbind(X, Y))
data$classes <- rep(c('X', 'Y'), times=nrow(X))
plot_idx <- sample(nrow(data))
ggplot(data[plot_idx,], aes(x=V1, y=V2, color=classes)) +
geom_point(alpha=0.4, size=3)
You need to create the legend manually. This is not a big problem though. You can loop over the labels and create a legend entry for each. Here one may use a Line2D with a marker similar to the scatter as handle.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
X = np.random.normal(0, 1, [5000, 2])
Y = np.random.normal(0.5, 1, [5000, 2])
data = np.concatenate([X,Y])
classes = np.concatenate([np.repeat('X', X.shape[0]),
np.repeat('Y', Y.shape[0])])
plot_idx = np.random.permutation(data.shape[0])
colors,labels = pd.factorize(classes)
fig, ax = plt.subplots()
sc = ax.scatter(data[plot_idx, 0],
data[plot_idx, 1],
c=colors[plot_idx],
alpha=0.4)
h = lambda c: plt.Line2D([],[],color=c, ls="",marker="o")
plt.legend(handles=[h(sc.cmap(sc.norm(i))) for i in range(len(labels))],
labels=list(labels))
plt.show()
Alternatively you can use a special scatter handler, as shown in the quesiton Why doesn't the color of the points in a scatter plot match the color of the points in the corresponding legend? but that seems a bit overkill here.
It's a bit of a hack, but you can save the axis limits, set the labels by drawing points well outside the limits of the plot, and then resetting the axis limits as follows:
plot_idx = np.random.permutation(data.shape[0])
color_idx, unique_classes = pd.factorize(classes)
colors = cm.Set1(np.linspace(0, 1, len(unique_classes)))
fig, ax = plt.subplots()
ax.scatter(data[plot_idx, 0],
data[plot_idx, 1],
c=colors[color_idx[plot_idx]],
alpha=0.4)
xlim = ax.get_xlim()
ylim = ax.get_ylim()
for i in range(len(unique_classes)):
ax.scatter(xlim[1]*10,
ylim[1]*10,
c=colors[i],
label=unique_classes[i])
ax.set_xlim(xlim)
ax.set_ylim(ylim)
plt.legend()
plt.show()

Set yaxis of all subplots to the same range - Matplotlib

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')
Your 1st line
f, axes = plt.subplots(7, 1, sharex='col', sharey='row', figsize=(15, 30))
has an inappropriate value for the sharey parameter. Using sharey='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 or sharey='col' you'll get what you want.
Addendum
The following code
In [34]: a = np.random.random(21)
In [35]: b = a+5
In [36]: x = np.arange(21)
In [37]: f, (ax, bx) = plt.subplots(2,1,sharey='row') # like yours
In [38]: ax.plot(x,a)
Out[38]: [<matplotlib.lines.Line2D at 0x7f5b98004f98>]
In [39]: bx.plot(x,b)
Out[39]: [<matplotlib.lines.Line2D at 0x7f5b980238d0>]
In [40]: f, (ax, bx) = plt.subplots(2,1,sharey='col') # like mine
In [41]: ax.plot(x,a)
Out[41]: [<matplotlib.lines.Line2D at 0x7f5b94764dd8>]
In [42]: bx.plot(x,b)
Out[42]: [<matplotlib.lines.Line2D at 0x7f5b98038198>]
In [43]:
gives me the following two plots. Can you spot a single difference?
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
axes[i].set_title('Simulation '+str(i+1), fontsize=20)
add:
axes[i].set_ylim(-1,1)
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.

Categories