Python教程

Python绘制基本图形——基于Spyder的matplotlib基本绘图(课程笔记)

本文主要是介绍Python绘制基本图形——基于Spyder的matplotlib基本绘图(课程笔记),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Python绘制基本图形——基于Spyder的matplotlib基本绘图(课程笔记)

使用的软件是Spyder

在这里插入图片描述

1、 折线图

import numpy as np
import matplotlib.pyplot as mp

x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([12, 34, 56, 75, 32, 4])
mp.plot(x, y)
mp.show() # 显示图表

img

2、 绘制水平线或垂直线

import numpy as np
import matplotlib.pyplot as mp
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([12, 34, 56, 75, 32, 4])

mp.vlines(4,10,35) # 绘制垂直线
mp.hlines(20,2,6) # 绘制水平线
mp.plot(x, y)
mp.show()

img

绘制多条长度不同的线

import numpy as np
import matplotlib.pyplot as mp
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([12, 34, 56, 75, 32, 4])

mp.vlines(4,10,35)
mp.hlines([10, 20, 30, 40],[1, 2, 3, 4],[6, 5, 4, 3])
mp.plot(x, y)
mp.show()

img

3、绘制弦曲线

3.1 正弦曲线

import numpy as np
import matplotlib.pyplot as mp

# 线性拆分1000个点
x = np.linspace(-np.pi, np.pi, 1000) 
y = np.sin(x)

mp.plot(x, y)
mp.show()

img

3.2 余弦曲线

import numpy as np
import matplotlib.pyplot as mp

# 线性拆分1000个点
x = np.linspace(-np.pi, np.pi, 1000) 
sinx = np.sin(x)
# 余弦曲线 cos(x)/2
cosx = np.cos(x)/2

mp.plot(x, sinx)
mp.plot(x, cosx)
mp.show()

img

4、线形、线宽、颜色、透明度

img

import numpy as np
import matplotlib.pyplot as mp

# 线性拆分1000个点
x = np.linspace(-np.pi, np.pi, 1000) 
sinx = np.sin(x)
# 余弦曲线 cos(x)/2
cosx = np.cos(x)/2

mp.plot(x, sinx,linestyle="--",linewidth=2,alpha=0.5) # alpha表示透明度
mp.plot(x, cosx,linestyle=":", color="green")
mp.show()

img

颜色配置

常用颜色、中国传统颜色及多种颜色配色器

https://blog.csdn.net/ziixiaoshenwang/article/details/119831428?spm=1001.2014.3001.5501

5、设置坐标轴范围

img

import numpy as np
import matplotlib.pyplot as mp

# 线性拆分1000个点
x = np.linspace(-np.pi, np.pi, 1000) 
sinx = np.sin(x)
# 余弦曲线 cos(x)/2
cosx = np.cos(x)/2
# 设置坐标轴的范围
mp.xlim(0, np.pi)
mp.ylim(0, 1)

mp.plot(x, sinx,linestyle="--",linewidth=2,alpha=0.5)
mp.plot(x, cosx,linestyle=":", color="green")
mp.show()

img

6、修改刻度文本

img

import numpy as np
import matplotlib.pyplot as mp

# 线性拆分1000个点
x = np.linspace(-np.pi, np.pi, 1000) 
sinx = np.sin(x)
# 余弦曲线 cos(x)/2
cosx = np.cos(x)/2
# 修改x轴的刻度文本
vals = [-np.pi, -np.pi/2, 0, np.pi/2, np.pi]
texts = [r'$-\pi$',r'$-\frac{\pi}{2}$','0',r'$\pi$',r'$\frac{\pi}{2}$'] #-π,-π/2,0,π/2,π#
mp.xticks(vals, texts)

mp.plot(x, sinx,linestyle="--",linewidth=2,alpha=0.5)
mp.plot(x, cosx,linestyle=":", color="green")
mp.show()

img

7、设置坐标轴

img

import numpy as np
import matplotlib.pyplot as mp

# 线性拆分1000个点
x = np.linspace(-np.pi, np.pi, 1000) 
sinx = np.sin(x)
# 余弦曲线 cos(x)/2
cosx = np.cos(x)/2
# 修改x轴的刻度及文本
vals = [-np.pi, -np.pi/2, 0, np.pi/2, np.pi]
texts = [r'$-\pi$',r'$-\frac{\pi}{2}$','0',r'$\pi$',r'$\frac{\pi}{2}$']
mp.xticks(vals, texts)
# 设置y轴的刻度
mp.yticks([-1.0,-0.5,0,0.5,1.0])
# 修改坐标轴
ax = mp.gca()
ax.spines['top'].set_color('none') # no表示上轴不显示
ax.spines['right'].set_color('none')
ax.spines['left'].set_position(('data', 0)) 
ax.spines['bottom'].set_position(('data',0))

mp.plot(x, sinx,linestyle="--",linewidth=2,alpha=0.5)
mp.plot(x, cosx,linestyle=":", color="green")
mp.show()

在这里插入图片描述
该内容为课程笔记

https://www.bilibili.com/video/BV1Yo4y197bN?p=20&spm_id_from=pageDriver

这篇关于Python绘制基本图形——基于Spyder的matplotlib基本绘图(课程笔记)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!