I'm trying to plot some subplots and I can't seem to share axes. I've seen other code and they seem to be doing exactly as I'm attempting but mine doesn't appear to do anything.
I simply want to share the respective axes in the four subplots on the left while keeping the subplot on the far right separate.
import numpy as np
import matplotlib.pyplot as plt
# set the data
x_data = np.linspace(0, 10)
y_data_1 = np.sin(x_data)
y_data_2 = np.cos(x_data)
y_data_3 = [i / 2 for i in y_data_1]
y_data_4 = [j / 2 for j in y_data_2]
# make the plot
ax1 = plt.subplot(2,3,1)
plt.plot(x_data, y_data_1)
ax2 = plt.subplot(2,3,2, sharey=ax1)
plt.plot(x_data, y_data_1)
plt.plot(x_data, y_data_2)
ax3 = plt.subplot(1,3,3)
plt.plot(x_data)
ax4 = plt.subplot(2,3,4, sharex=ax1)
plt.plot(x_data, y_data_1)
plt.plot(x_data, y_data_2)
plt.plot(x_data, y_data_3)
ax5 = plt.subplot(2,3,5, sharex=ax2, sharey=ax4)
plt.plot(x_data, y_data_1)
plt.plot(x_data, y_data_2)
plt.plot(x_data, y_data_3)
plt.plot(x_data, y_data_4)
plt.show()
As you can see, despite adding sharex and sharey arguments into the plt.subplot commands the plot returned shows all unique subplots.
I'm sure I'm making a minor error, but I've tried this in the past and remember that I had the same issue! Any help appreciated, as well as recommendations for alternative approaches. Even the website contains various ways of doing the same thing and it is a little confusing.
You're almost there. You are sharing axes correctly, but need to make the axes invisible (as instructed by the Shared Axis Demo) for some subplots:
plt.setp(ax1.get_xticklabels(), visible=False)
In your code, this might look like:
# make the plot
ax1 = plt.subplot(2, 3, 1)
plt.plot(x_data, y_data_1)
plt.setp(ax1.get_xticklabels(), visible=False)
ax2 = plt.subplot(2, 3, 2, sharey=ax1)
plt.plot(x_data, y_data_1)
plt.plot(x_data, y_data_2)
plt.setp(ax2.get_xticklabels(), visible=False)
plt.setp(ax2.get_yticklabels(), visible=False)
ax3 = plt.subplot(1, 3, 3)
plt.plot(x_data)
ax4 = plt.subplot(2, 3, 4, sharex=ax1)
plt.plot(x_data, y_data_1)
plt.plot(x_data, y_data_2)
plt.plot(x_data, y_data_3)
ax5 = plt.subplot(2, 3, 5, sharex=ax2, sharey=ax4)
plt.plot(x_data, y_data_1)
plt.plot(x_data, y_data_2)
plt.plot(x_data, y_data_3)
plt.plot(x_data, y_data_4)
plt.setp(ax5.get_yticklabels(), visible=False)
plt.show()
That code, plus imports / declared x and y data, results in:
However, there is an even better demo here for creating subplots with shared axes. The best solutions that I saw for shared axes used the .subplots()
function to improve the readability / simplicity of the code, e.g.:
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row')
Best of luck.
The following would be a solution using plt.subplots
. The idea is to turn the rightmost 2 axes of a 2x3 grid invisble and instead create a new subplot at their position.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10)
fig, ((ax1, ax2, aux1), (ax3, ax4, aux2)) = plt.subplots(2, 3, sharex=True, sharey=True)
#turn rightmost 2 axes off
aux1.axis("off")
aux2.axis("off")
#create a new subplot at their position
ax5 = fig.add_subplot(133)
#plot stuff on the axes
ax1.plot(x,np.sin(x))
ax2.plot(x,np.sin(x))
ax3.plot(x,np.sin(x))
ax4.plot(x,np.sin(x))
ax5.plot(5*x,x)
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With