文章目录
Python–Matplotlib(基本用法)
python绘制三维图
一图多线
画一条线plot(x,y3,color='#900302',marker='+',linestyle='-')
画多条线 plt.plot(x,y,"bo-",x,z,"g1--",x,m,"kv:")
"g1–"
颜色(color),点型(marker),线型(linestyle),
‘[color][marker][line]’
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1,11,2)
print(x)
y = np.sin(x)
z =2*np.cos(10*x)
m =2*x+3
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("测试")
plt.plot(x,y,"bo-",x,z,"g1--",x,m,"kv:")
plt.show()
============= ===============================
character 颜色(color)
============= ===============================
``'b'`` blue 蓝
``'g'`` green 绿
``'r'`` red 红
``'c'`` cyan 蓝绿
``'m'`` magenta 洋红
``'y'`` yellow 黄
``'k'`` black 黑
``'w'`` white 白
============= ===============================
============= ===============================
character 点型(marker)
============= ===============================
``'.'`` point marker
``','`` pixel marker
``'o'`` circle marker
``'v'`` triangle_down marker
``'^'`` triangle_up marker
``'<'`` triangle_left marker
``'>'`` triangle_right marker
``'1'`` tri_down marker
``'2'`` tri_up marker
``'3'`` tri_left marker
``'4'`` tri_right marker
``'s'`` square marker
``'p'`` pentagon marker
``'*'`` star marker
``'h'`` hexagon1 marker
``'H'`` hexagon2 marker
``'+'`` plus marker
``'x'`` x marker
``'D'`` diamond marker
``'d'`` thin_diamond marker
``'|'`` vline marker
``'_'`` hline marker
============= ===============================
============= ===============================
character 线型(linestyle)
============= ===============================
``'-'`` solid line style 实线
``'--'`` dashed line style 虚线
``'-.'`` dash-dot line style 点画线
``':'`` dotted line style 点线
============= ===============================
参考线、网格线、数学函数
三角:sin、cos、tan、
指数:exp
对数:log、log10
开方:sqrt
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,5,1000)
print(x)
y = np.log(x)
plt.plot(x,y,"b")
plt.hlines(2,0,6,linestyle='--')
plt.vlines(4,-3,4,linestyle='--')
plt.grid(alpha=0.4,linestyle='--')
plt.show()
动态图
import numpy as np
import matplotlib.pyplot as plt
N = 20
plt.close() # 关闭打开的图形窗口
def anni():
fig = plt.figure()
plt.ion() # 打开交互式绘图interactive
for i in range(N):
plt.cla() # 清除原有图像
plt.xlim(-0.2,20.4) # 设置x轴坐标范围
plt.ylim(-1.2,1.2) # 设置y轴坐标范围
# 每当i增加的时候,增加自变量x的区间长度,可以理解为不断叠加绘图,所以每次循环之前都使用plt.cla()命令清除原有图像
x = np.linspace(0,i+1,1000)
y = np.sin(x)
plt.plot(x,y)
plt.pause(0.1)
plt.ioff() #关闭交互式绘图
plt.show()
anni()