栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Matplotlib Python 画图教程

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

运行:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/339536.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号