1.figure图像
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 60)
y1 = 2*x+1
y2 = x**2
#the first figure
plt.figure()
plt.plot(x, y1, color='green',linewidth=3.0)
#the second figure
#num is title
plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y2, color='green')
plt.plot(x, y1, color='red', linewidth=3.0, linestyle='--')
plt.show()
显示:
 
2.
设置坐标轴1
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 60)
y1 = 2*x+1
y2 = x**2
plt.figure()
plt.plot(x, y1, color='green',linewidth=3.0)
#num is title
plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y2, color='green')
plt.plot(x, y1, color='red', linewidth=3.0, linestyle='--')
plt.xlim(-1, 2)
plt.ylim(-2, 3)
plt.xlabel('I am X')
plt.ylabel('I am Y')
# from -1 to 2 show total 5 numbers in x
new_ticks = np.linspace(-1, 2, 5)
print(new_ticks)
plt.xticks(new_ticks)
#
plt.yticks([-2, -1, 8, 1.22, 3], ['really bad', 'bad', 'normal', 'good', 'really good'])
#$ represent need read position
plt.yticks([-2, -1, 8, 1.22, 3], [r'$really\ bad$', '$bad\\alpha$', '$normal$', '$good$', '$really\ good$'])
plt.show()
运行:

设置坐标轴2
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 60)
y1 = 2*x+1
y2 = x**2
plt.figure()
plt.plot(x, y1, color='green',linewidth=3.0)
#num is title
plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y2, color='green')
plt.plot(x, y1, color='red', linewidth=3.0, linestyle='--')
plt.xlim(-1, 2)
plt.ylim(-2, 3)
plt.xlabel('I am X')
plt.ylabel('I am Y')
# from -1 to 2 total 5 numbers
new_ticks = np.linspace(-1, 2, 5)
print(new_ticks)
plt.xticks(new_ticks)
#
plt.yticks([-2, -1, 8, 1.22, 3], ['really bad', 'bad', 'normal', 'good', 'really good'])
#$ represent need read position
plt.yticks([-2, -1, 8, 1.22, 3], [r'$really\ bad$', '$bad\\alpha$', '$normal$', '$good$', '$really\ good$'])
#gca = 'get current axis'
ax = plt.gca()
# set (0,0) origin
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
plt.show()
运行:

3.图例
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 60)
y1 = 2*x+1
y2 = x**2
plt.figure()
plt.plot(x, y1, color='green',linewidth=3.0)
#num is title
plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y2, color='green')
plt.plot(x, y1, color='red', linewidth=3.0, linestyle='--')
plt.xlim(-1, 2)
plt.ylim(-2, 3)
plt.xlabel('I am X')
plt.ylabel('I am Y')
# from -1 to 2 total 5 numbers
new_ticks = np.linspace(-1, 2, 5)
print(new_ticks)
plt.xticks(new_ticks)
#
plt.yticks([-2, -1, 8, 1.22, 3], ['really bad', 'bad', 'normal', 'good', 'really good'])
#$ represent need read position
plt.yticks([-2, -1, 8, 1.22, 3], [r'$really\ bad$', '$bad\\alpha$', '$normal$', '$good$', '$really\ good$'])
#gca = 'get current axis'
ax = plt.gca()
#
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
plt.plot(x, y2, label='up')
plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--', label='down')
plt.legend()
plt.show()
运行:

给图例加名字,自动寻找位置
#author_='飞天小女警';
#date: 2021/2/22 15:02
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 60)
y1 = 2*x+1
y2 = x**2
plt.figure()
plt.plot(x, y1, color='green',linewidth=3.0)
#num is title
plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y2, color='green')
plt.plot(x, y1, color='red', linewidth=3.0, linestyle='--')
plt.xlim(-1, 2)
plt.ylim(-2, 3)
plt.xlabel('I am X')
plt.ylabel('I am Y')
# from -1 to 2 total 5 numbers
new_ticks = np.linspace(-1, 2, 5)
print(new_ticks)
plt.xticks(new_ticks)
#
plt.yticks([-2, -1, 8, 1.22, 3], ['really bad', 'bad', 'normal', 'good', 'really good'])
#$ represent need read position
plt.yticks([-2, -1, 8, 1.22, 3], [r'$really\ bad$', '$bad\\alpha$', '$normal$', '$good$', '$really\ good$'])
#gca = 'get current axis'
ax = plt.gca()
#
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
l1, = plt.plot(x, y2, label='up')
l2, = plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--', label='down')
#if you want to deliver l1 and l2 to handles,you should set "," to l1,l2
plt.legend(handles=[l1, l2], labels=['aaa', 'bbb'], loc='best')
plt.show()
运行:

4.Annotation 标注
#author_='飞天小女警';
#date: 2021/2/22 15:02
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 60)
y1 = 2*x+1
y2 = x**2
plt.figure()
plt.plot(x, y1, color='green',linewidth=3.0)
#num is title
plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y2, color='green')
plt.plot(x, y1, color='red', linewidth=3.0, linestyle='--')
plt.xlim(-1, 2)
plt.ylim(-2, 3)
plt.xlabel('I am X')
plt.ylabel('I am Y')
# from -1 to 2 total 5 numbers
new_ticks = np.linspace(-1, 2, 5)
print(new_ticks)
plt.xticks(new_ticks)
#
plt.yticks([-2, -1, 8, 1.22, 3], ['really bad', 'bad', 'normal', 'good', 'really good'])
#$ represent need read position
plt.yticks([-2, -1, 8, 1.22, 3], [r'$really\ bad$', '$bad\\alpha$', '$normal$', '$good$', '$really\ good$'])
#gca = 'get current axis'
ax = plt.gca()
#
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
l1, = plt.plot(x, y2, label='up')
l2, = plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--', label='down')
#if you want to deliver l1 and l2 to handles,you should set "," to l1,l2
plt.legend(handles=[l1, l2], labels=['aaa', 'bbb'], loc='best')
#Annotation
x0 = 1
y0 = 2*x0 + 1
plt.scatter(x0, y0, s=50, color='b')
#left--right[x0, x0] higth--bottom [y0, 0]
plt.plot([x0, x0], [y0, 0], 'k--', lw=2.5)
plt.show()
运行:

|