2

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 !

asked Jun 28, 2020 at 4:33

3 Answers 3

2

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()
answered Jun 28, 2020 at 5:05
1

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 :)

answered Jun 28, 2020 at 4:42
1

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

answered Jun 28, 2020 at 4:50
3
  • 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 it Commented 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/… Commented Jul 10, 2020 at 5:19
  • Thanks, This clarifies Commented Jul 11, 2020 at 16:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.