Shawn Zhang - 1 year ago 103

Python Question

I am having quite a bit of trouble understanding how to create good subplots. I want to create a figure that is similar to the one shown below. Does anyone know how I could set up a similar template as this?

Also, how would I include these points with error bars in the subplots?

This is my code for the error bars:

`mass, p, errp, errl = np.loadtxt('/Users/shawn/Desktop/vika1.dat', usecols = [0, 10, 11, 12], unpack = True)`

plt.errorbar(mass, np.log10(p) - 4, yerr = [np.log10(p) - np.log10(p-errl), np.log10(p + errp) - np.log10(p)], fmt = 'o', markerfacecolor = 'w', markeredgecolor = 'k', ecolor = 'k')

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You could use sharex and sharey to share the axes. The following will give the layout you want. You can then plot individual subplots using your specific plot funcitons.

Updated complete code below

```
import numpy as np
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)
X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C, S = np.cos(X), np.sin(X)
axes[0,0].plot(X, C, color="blue", linewidth=1.0, linestyle="-")
axes[0,1].plot(X, C, color="orange", linewidth=1.0, linestyle="-")
axes[1,0].plot(X, C, color="green", linewidth=1.0, linestyle="-")
axes[1,1].plot(X, C, color="red", linewidth=1.0, linestyle="-")
plt.subplots_adjust(wspace=0,hspace=0)
plt.show()
```

Can't understand why someone has downvoted me for the initial answer...

The below lines would prune the min value for both x and y axes thereby avoiding label overlaps

```
from matplotlib.ticker import MaxNLocator
axes[1,1].yaxis.set_major_locator(MaxNLocator(prune='lower'))
axes[1,1].xaxis.set_major_locator(MaxNLocator(prune='lower'))
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**