I have 4 figures (y1,y2,y3,y4) that i want to plot on a common x axis (yr1,yr2,yr3,m1,m2,m3,m4,m5). In this code however i have kept axaxis as separate since i am trying to get the basics right first.
import matplotlib.pyplot as plt
import numpy as np
plt.figure(1)
xaxis = ['y1','y2','y3','m1','m2','m3', 'm4', 'm5']
y1 = np.array([.73,.74,.71,.75,.72,.75,.74,.74])
y2 = np.array([.82,.80,.77,.81,.72,.81,.77,.77])
y3 = np.array([.35,.36,.45,.43,.44,.45,.48,.45])
y4 = np.array([.49,.52,.59,.58,.61,.65,.61,.58])
plt.subplot(221)
plt.plot(xaxis,y1)
plt.subplot(222)
plt.plot(xaxis,y2)
plt.subplot(223)
plt.subplot(xaxis,y3)
plt.subplot(224)
plt.subplot(xaxis,y4)
plt.show()
Getting this error
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-23-dfe04cc8c6c4> in <module>
14 plt.plot(xaxis,y2)
15 plt.subplot(223)
---> 16 plt.subplot(xaxis,y3)
17 plt.subplot(224)
18 plt.subplot(xaxis,y4)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\pyplot.py in subplot(*args, **kwargs)
1074
1075 fig = gcf()
-> 1076 a = fig.add_subplot(*args, **kwargs)
1077 bbox = a.bbox
1078 byebye = []
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\figure.py in add_subplot(self, *args, **kwargs)
1412 self._axstack.remove(ax)
1413
-> 1414 a = subplot_class_factory(projection_class)(self, *args, **kwargs)
1415
1416 return self._add_axes_internal(key, a)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_subplots.py in __init__(self, fig, *args, **kwargs)
62 # num - 1 for converting from MATLAB to python indexing
63 else:
---> 64 raise ValueError(f'Illegal argument(s) to subplot: {args}')
65
66 self.update_params()
ValueError: Illegal argument(s) to subplot: (['y1', 'y2', 'y3', 'm1', 'm2', 'm3', 'm4', 'm5'], array([0.35, 0.36, 0.45, 0.43, 0.44, 0.45, 0.48, 0.45]))
Please help understand the issue here !
3 Answers 3
Reusing Sahith Kurapati's code just to provide a cleaner solution. This way you can share axes and only configure line and chart styles once if they're all supposed to have the same style.
import matplotlib.pyplot as plt
import numpy as np
plt.figure(1)
xaxis = ['y1','y2','y3','m1','m2','m3', 'm4', 'm5']
yy = np.array([[.73,.74,.71,.75,.72,.75,.74,.74],
[.82,.80,.77,.81,.72,.81,.77,.77],
[.35,.36,.45,.43,.44,.45,.48,.45],
[.49,.52,.59,.58,.61,.65,.61,.58]])
fig, axes = plt.subplots(2, 2)
for y, ax in zip(yy, axes.ravel()):
ax.plot(y)
plt.show()
Small mistake. You have put plt.subplot
instead of plt.plot
. This should work now:
import matplotlib.pyplot as plt
import numpy as np
plt.figure(1)
xaxis = ['y1','y2','y3','m1','m2','m3', 'm4', 'm5']
y1 = np.array([.73,.74,.71,.75,.72,.75,.74,.74])
y2 = np.array([.82,.80,.77,.81,.72,.81,.77,.77])
y3 = np.array([.35,.36,.45,.43,.44,.45,.48,.45])
y4 = np.array([.49,.52,.59,.58,.61,.65,.61,.58])
plt.subplot(221)
plt.plot(xaxis,y1)
plt.subplot(222)
plt.plot(xaxis,y2)
plt.subplot(223)
plt.plot(xaxis,y3)
plt.subplot(224)
plt.plot(xaxis,y4)
plt.show()
Hope this helps :)
Try this:
fig, ax = plt.subplots(4, 1,sharex=True,gridspec_kw= {'height_ratios':[3,1,1,1]})
ax[0].plot(xais,y1)
ax[1].plot(xais,y1)
ax[2].plot(xais,y1)
ax[3].plot(xais,y1)
for 4 figures stacked on top of each other with shared x-axis.
for 2x2:
fig, ax = plt.subplots(2,2)
ax[0,0].plot(xaxis,y1)
ax[0,1].plot(xaxis,y1)
ax[1,0].plot(xaxis,y1)
ax[1,1].plot(xaxis,y1)
and then plt.show() to see results.
Look here for more: For more here is an example
-
Thanks a ton @user3850153 ! what are fig and ax. Looks like ax represent different graphs on the 2 x 2 plot for e.g. used ax[0,0].set_title('Fig 1') but what is "fig". How do we use itGupta– Gupta2020年06月28日 11:50:04 +00:00Commented Jun 28, 2020 at 11:50
-
fig represents your whole figure. If you have another fig (running this command again) two windows will open. ax is an array of plots inside the figure. It could also be used like this: fig, ((ax1, ax2), (ax3, ax4)) = plt.subplot(2, 2) matplotlib.org/3.2.2/api/_as_gen/… matplotlib.org/3.2.2/api/_as_gen/…Hamidreza– Hamidreza2020年07月10日 05:19:55 +00:00Commented Jul 10, 2020 at 5:19
-