/ Python And R Data science skills: 73 subplots oop

Sunday, 18 February 2018

73 subplots oop

https://vlrtraining.com/courses/python-data-science-beginner-tutorial 73 subplots oop
In [27]:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 5, 11)
y = x ** 2 
%matplotlib inline
#fig = plt.figure()
x
y
Out[27]:
array([  0.  ,   0.25,   1.  ,   2.25,   4.  ,   6.25,   9.  ,  12.25,
        16.  ,  20.25,  25.  ])
In [32]:
f,a = plt.subplots(1,2)
plt.tight_layout()

# Now use the axes object to add stuff to plot
#axes.plot(x, y, 'r')
#axes.set_xlabel('x')
#axes.set_ylabel('y')
#axes.set_title('title');
a
Out[32]:
array([<matplotlib.axes._subplots.AxesSubplot object at 0x000000000A791780>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x000000000AF03D68>], dtype=object)
In [36]:
f,a = plt.subplots(nrows=1, ncols=2)
a[0].plot(x,y)
a[1].plot(y,x)
a[0].set_title("first")
a[0].set_xlabel("my x label")
Out[36]:
Text(0.5,0,'my x label')
In [20]:
a
Out[20]:
array([<matplotlib.axes._subplots.AxesSubplot object at 0x000000000ABC9470>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x000000000AC922E8>], dtype=object)
In [18]:
a
Out[18]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000000000898DDD8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000A4432E8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000000A47C2E8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000A4B4828>]], dtype=object)
In [19]:
f,a = plt.subplots(nrows=1, ncols=2)
In [9]:
f,a = plt.subplots(nrows=2, ncols=2)
plt.tight_layout()
In [5]:
for ax in axes:
    ax.plot(x, y, 'b')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_title('title')

# Display the figure object    
fig
Out[5]:

1 comment:

  1. for ax in axes:
    ax.plot(x, y, 'b')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_title('title')

    # Display the figure object
    fig

    sir i got this error

    AttributeError Traceback (most recent call last)
    in ()
    1 for ax in axes:
    ----> 2 ax.plot(x, y, 'b')
    3 ax.set_xlabel('x')
    4 ax.set_ylabel('y')
    5 ax.set_title('title')

    AttributeError: 'Figure' object has no attribute 'plot'

    ReplyDelete