这周学习了Python画图工具发现plot()里面有很多参数所以决定整理总结一下
先上一个简单的图像
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 1.2, 0.1)
y = 2 * x + 1
z = x ** 2
# 创建画布
plt.figure(figsize=(8, 6))
# 设定字体
plt.rcParams['font.sans-serif'] = 'SimHei'
# 曲线
plt.plot(x, z, markersize=5, marker='o', color='green', markerfacecolor='blue', linestyle='dashed', linewidth=1, markeredgecolor='m')
plt.plot(x, y, markersize=5, marker='o', color='green', markerfacecolor='blue', linestyle='dashed', linewidth=1, markeredgecolor='m')
plt.title('lines')
plt.xticks([0, 0.2, 0.4, 0.6, 0.8, 1, 1.1, 1.2]) # 刻度
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.show()
重点介绍plot函数的几个参数
color:控制线条颜色
控制选择线条的颜色
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 1.2, 0.1)
y = 2 * x + 1
z = x ** 2
# 创建画布
plt.figure(figsize=(8, 6))
# 设定字体
plt.rcParams['font.sans-serif'] = 'SimHei'
# 曲线
plt.plot(x, z, color='firebrick')
plt.plot(x, y, color='slateblue')
plt.show()
linestyle:线条风格
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 1.2, 0.1)
y = 2 * x + 1
z = x ** 2
# 创建画布
plt.figure(figsize=(8, 6))
# 设定字体
plt.rcParams['font.sans-serif'] = 'SimHei'
# 曲线
plt.plot(x, z, linestyle=':')
plt.plot(x, y, linestyle='-.')
plt.show()
marker:标记风格
marker | symbol | description |
---|---|---|
"." |
![]() |
point |
"," |
![]() |
pixel |
"o" |
![]() |
circle |
"v" |
![]() |
triangle_down |
"^" |
![]() |
triangle_up |
"<" |
![]() |
triangle_left |
">" |
![]() |
triangle_right |
"1" |
![]() |
tri_down |
"2" |
![]() |
tri_up |
"3" |
![]() |
tri_left |
"4" |
![]() |
tri_right |
"8" |
![]() |
octagon |
"s" |
![]() |
square |
"p" |
![]() |
pentagon |
"P" |
![]() |
plus (filled) |
"*" |
![]() |
star |
"h" |
![]() |
hexagon1 |
"H" |
![]() |
hexagon2 |
"+" |
![]() |
plus |
"x" |
![]() |
x |
"X" |
![]() |
x (filled) |
"D" |
![]() |
diamond |
"d" |
![]() |
thin_diamond |
"|" |
![]() |
vline |
"_" |
![]() |
hline |
0 (TICKLEFT ) |
![]() |
tickleft |
1 (TICKRIGHT ) |
![]() |
tickright |
2 (TICKUP ) |
![]() |
tickup |
3 (TICKDOWN ) |
![]() |
tickdown |
4 (CARETLEFT ) |
![]() |
caretleft |
5 (CARETRIGHT ) |
![]() |
caretright |
6 (CARETUP ) |
![]() |
caretup |
7 (CARETDOWN ) |
![]() |
caretdown |
8 (CARETLEFTBASE ) |
![]() |
caretleft (centered at base) |
9 (CARETRIGHTBASE ) |
![]() |
caretright (centered at base) |
10 (CARETUPBASE ) |
![]() |
caretup (centered at base) |
11 (CARETDOWNBASE ) |
![]() |
caretdown (centered at base) |
"None" , " " or "" |
nothing | |
'$...$' |
![]() |
Render the string using mathtext. E.g "$f$" for marker showing the letter f . |
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 1.2, 0.1)
y = 2 * x + 1
z = x ** 2
# 创建画布
plt.figure(figsize=(8, 6))
# 设定字体
plt.rcParams['font.sans-serif'] = 'SimHei'
# 曲线
plt.plot(x, z, markersize=10, marker='*')
plt.plot(x, y, markersize=10, marker=0)
plt.show()
markerfacecolor:标记点颜色
设定画图中点的颜色
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 1.2, 0.1)
y = 2 * x + 1
z = x ** 2
# 创建画布
plt.figure(figsize=(8, 6))
# 设定字体
plt.rcParams['font.sans-serif'] = 'SimHei'
# 曲线
plt.plot(x, z, markersize=10, marker='*', markerfacecolor='lawngreen')
plt.plot(x, y, markersize=10, marker='o', markerfacecolor='navy')
plt.show()
markersize:标记尺寸
设定图中标记点的大小
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 1.2, 0.1)
y = 2 * x + 1
z = x ** 2
# 创建画布
plt.figure(figsize=(8, 6))
# 设定字体
plt.rcParams['font.sans-serif'] = 'SimHei'
# 曲线
plt.plot(x, z, markersize=10, marker='*', markerfacecolor='lawngreen')
plt.plot(x, y, markersize=5, marker='*', markerfacecolor='navy')
plt.show()
linewidth:线条宽度
设定线条的宽度
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 1.2, 0.1)
y = 2 * x + 1
z = x ** 2
# 创建画布
plt.figure(figsize=(8, 6))
# 设定字体
plt.rcParams['font.sans-serif'] = 'SimHei'
# 曲线
plt.plot(x, z, linewidth=0.5)
plt.plot(x, y, linewidth=2)
plt.show()
markeredgecolor:点边框颜色
设定点的边框的颜色