Using Matplotlib, I have two subplots and I want them to have the same custom string xticks.
Here is a minimal example what I tried so far:
import matplotlib.pyplot as plt
f, axs = plt.subplots(ncols=2, sharex=True)
plt.xticks(range(6), [str(x)+"foo" for x in range(6)], rotation='45')
for i in range(2):
ax = axs[i]
ax.plot(range(6), range(6))
f.show()
Produces this output:
Note that the xticks on the left are not rotated. How can I do that?
If I remove the sharex=True
, the left subplot has no custom xticks. However, I cannot give xticks to a single axis. It results in an error:
AttributeError: 'AxesSubplot' object has no attribute 'xticks'
We can use the plt. subplots_adjust() method to change the space between Matplotlib subplots. The parameters wspace and hspace specify the space reserved between Matplotlib subplots. They are the fractions of axis width and height, respectively.
To draw multiple plots using the subplot() function from the pyplot module, you need to perform two steps: First, you need to call the subplot() function with three parameters: (1) the number of rows for your grid, (2) the number of columns for your grid, and (3) the location or axis for plotting.
If sharex
is not principal use this code:
import matplotlib.pyplot as plt
f, axs = plt.subplots(ncols=2)
for i in range(2):
ax = axs[i]
ax.set_xticks(range(6))
ax.set_xticklabels([str(x)+"foo" for x in range(6)], rotation=45)
ax.plot(range(6), range(6))
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