### Matplotlib tutorial 02¶

In [1]:
# import
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

In [2]:
# generating some data points
X = np.linspace(-np.pi, np.pi, 20, endpoint=True)
C, S = np.cos(X), np.sin(X)

In [3]:
# Simply plotting these in same plot

plt.plot(X, C,
X, S)

Out[3]:
[<matplotlib.lines.Line2D at 0x790f278>,
<matplotlib.lines.Line2D at 0x790fc88>]
In [4]:
plt.plot(X, C,
X, C, 'oy',
X, S,
X, S, 'or')

Out[4]:
[<matplotlib.lines.Line2D at 0x79d2940>,
<matplotlib.lines.Line2D at 0x79d2b00>,
<matplotlib.lines.Line2D at 0x79d84a8>,
<matplotlib.lines.Line2D at 0x79d8cc0>]

Checking and Defining the Range of Axes

In [5]:
plt.plot(X, C,
X, C, 'oy',
X, S,
X, S, 'or')

print(plt.axis())  # this will print the current plotting X and Y values

(-4.0, 4.0, -1.0, 1.0)

In [6]:
# we can change it by assigning new one

plt.plot(X, C,
X, C, 'oy',
X, S,
X, S, 'or')

print(plt.axis())

x1, x2, y1, y2 = (-5, 5, -1.5, 1.5)
plt.axis([x1, x2, y1, y2])

print(plt.axis())

(-4.0, 4.0, -1.0, 1.0)
(-5.0, 5.0, -1.5, 1.5)


"linspace" to Define X Values

linspace can be used to create evenly spaced numbers over a specified interval.
linspace(start, stop, num=50, endpoint=True, retstep=False)   
In [7]:
X = np.linspace(0, 2 * np.pi, 20, endpoint=True)
F = np.sin(X)
plt.plot(X,F)
startx, endx = -0.1, 2*np.pi + 0.1
starty, endy = -1.1, 1.1
plt.axis([startx, endx, starty, endy])
plt.show()

In [8]:
X = np.linspace(-2 * np.pi, 2 * np.pi, 20, endpoint=True)
F1 = 3 * np.sin(X)
F2 = np.sin(2*X)
F3 = 0.3 * np.sin(X)
startx, endx = -2 * np.pi - 0.1, 2*np.pi + 0.1
starty, endy = -3.1, 3.1
plt.axis([startx, endx, starty, endy])
plt.plot(X,F1)
plt.plot(X,F2)
plt.plot(X,F3)
plt.show()

In [9]:
X = np.linspace(-2 * np.pi, 2 * np.pi, 20, endpoint=True)
F1 = 3 * np.sin(X)
F2 = np.sin(2*X)
F3 = 0.3 * np.sin(X)
startx, endx = -2 * np.pi - 0.1, 2*np.pi + 0.1
starty, endy = -3.1, 3.1
plt.axis([startx, endx, starty, endy])
plt.plot(X,F1)
plt.plot(X,F2)
plt.plot(X,F3)
plt.plot(X, F1, 'ro')
plt.plot(X, F2, 'bx')
plt.show()


Customizing Ticks

In [10]:
plt.plot(X, C,
X, S)

print(plt.xticks())
print(plt.yticks())

(array([-8., -6., -4., -2.,  0.,  2.,  4.,  6.,  8.]), <a list of 9 Text xticklabel objects>)
(array([-1. , -0.5,  0. ,  0.5,  1. ]), <a list of 5 Text yticklabel objects>)

Ticks actually holding the two things, 1 - ticks value , 2 - ticks label


Let's change the values first

In [11]:
plt.plot(X, C,
X, S)

plt.xticks([0, 1, 2, 4, 5])
plt.yticks([0, 1])

Out[11]:
([<matplotlib.axis.YTick at 0x7dcd5c0>, <matplotlib.axis.YTick at 0x7dc91d0>],
<a list of 2 Text yticklabel objects>)
In [12]:
plt.plot(X, C,
X, S)

plt.xticks(np.arange(-10, 10, 2))
plt.yticks(np.arange(-2, 2, 0.5))

Out[12]:
([<matplotlib.axis.YTick at 0x7e34630>,
<matplotlib.axis.YTick at 0x7e25f98>,
<matplotlib.axis.YTick at 0x7e2c390>,
<matplotlib.axis.YTick at 0x7e1e7f0>,
<matplotlib.axis.YTick at 0x7e7c2e8>,
<matplotlib.axis.YTick at 0x7e84940>,
<matplotlib.axis.YTick at 0x7e7c940>,
<matplotlib.axis.YTick at 0x7e87518>],
<a list of 8 Text yticklabel objects>)

Now changing the ticks lable

In [13]:
plt.plot(X, C,
X, S)

plt.xticks(np.arange(-10, 10, 2), ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
plt.yticks(np.arange(-2, 2, 0.5))

Out[13]:
([<matplotlib.axis.YTick at 0x7e2cba8>,
<matplotlib.axis.YTick at 0x7dbbc50>,
<matplotlib.axis.YTick at 0x8e762e8>,
<matplotlib.axis.YTick at 0x8eacda0>,
<matplotlib.axis.YTick at 0x8ec14a8>,
<matplotlib.axis.YTick at 0x8ec1eb8>,
<matplotlib.axis.YTick at 0x8ec6908>,
<matplotlib.axis.YTick at 0x8ec6b70>],
<a list of 8 Text yticklabel objects>)

Chnaging Spine
the gca function returns the current Axes instance on the current figure.

In [14]:
plt.plot(X, C,
X, S)

# getting current axis and spine instance
ax = plt.gca()
print(ax)

# making the top and right spine invisible:
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

Axes(0.125,0.125;0.775x0.775)

In [15]:
plt.plot(X, C,
X, S)

ax = plt.gca()

# making the top and right spine invisible:
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

# moving all top ticks tp bottom and right ticks to left
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

In [16]:
plt.plot(X, C,
X, S)

ax = plt.gca()

# making the top and right spine invisible:
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

# moving all top ticks tp bottom and right ticks to left
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

# moving bottom spine up to y=0 position:
ax.spines['bottom'].set_position(('data',0))

# moving left spine to the right to position x == 0:
ax.spines['left'].set_position(('data',0))

In [17]:
plt.plot(X, C,
X, S)

ax = plt.gca()

# making the top and right spine invisible:
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

# moving all top ticks tp bottom and right ticks to left
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

# moving bottom spine up to y=0 position:
ax.spines['bottom'].set_position(('data',0))

# moving left spine to the right to position x == 0:
ax.spines['left'].set_position(('data',0))

# setting ticks value
plt.xticks(np.arange(-8, 8, 2))
plt.yticks(np.arange(-2, 2, 0.5))

Out[17]:
([<matplotlib.axis.YTick at 0x8fe55f8>,
<matplotlib.axis.YTick at 0x8fe5048>,
<matplotlib.axis.YTick at 0x9000710>,
<matplotlib.axis.YTick at 0x9004438>,
<matplotlib.axis.YTick at 0x9039470>,
<matplotlib.axis.YTick at 0x9037390>,
<matplotlib.axis.YTick at 0x9037fd0>,
<matplotlib.axis.YTick at 0x9041f28>],
<a list of 8 Text yticklabel objects>)