Matplotlib Python 画图教程

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

运行:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值