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]:
In [4]:
plt.plot(X, C,
X, C, 'oy',
X, S,
X, S, 'or')
Out[4]:
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
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())
"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())
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]:
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]:
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]:
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')
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]:
In [ ]:
In [ ]: