02 Set chart style
#linewidth 绘制线条宽度
plt.plot(a,s, linewidth = 6)
#添加x,y轴名称
plt.xlabel('x')
plt.ylabel('y = x^2')
#给图标添加图名
plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文下的字体格式进行修改对
plt.title('多点绘制')
plt.show()
Draw lines in different styles and colors
And use the legend () method to add the label parameter to the plot
x = np.linspace(0, 10, 100)
plt.plot(x, x+0, '--g', label = "--g")
plt.plot(x, x+1, '-.r', label = '-.r')
plt.plot(x, x+2, ':b', label = ':b')
plt.plot(x, x+3, ',c', label = ',c')
plt.plot(x, x+4, '*y', label = '*y')
plt.plot(x, x+5, '.k', label = '.k')
plt.legend(loc = 'lower right', # 默认在左上角即 upper left 可以通过loc进行修改
fancybox = True, # 边框
framealpha = 0.5, # 透明度
shadow = True, # 阴影
borderpad = 1) # 边框宽度
plt.show()
Format character
format | style |
---|---|
‘-’ | Solid line style |
‘–’ | Dash style |
‘-.’ | Dotted style |
‘:’ | Dotted style |
‘.’ | Dot mark |
‘,’ | Pixel mark |
'O' | Round mark |
'v' | Inverted triangle mark |
‘^’ | Positive triangle mark |
‘<’ | Left triangle mark |
‘>’ | Right triangle mark |
‘1’ | Down arrow mark |
‘2’ | Up arrow mark |
‘3’ | Left arrow mark |
‘4’ | Right arrow mark |
‘s’ | Square mark |
‘p’ | Pentagon mark |
‘*’ | Star mark |
‘h’ | Hexagon mark 1 |
‘H’ | Hexagon mark 2 |
‘+’ | Plus sign |
‘x’ | X mark |
‘D’ | Diamond mark |
‘d’ | Narrow diamond mark |
‘|’ | Vertical line marking |
‘_’ | Horizontal mark |
Common colors
Code | color |
---|---|
‘b’ | blue |
‘g’ | green |
‘r’ | red |
‘c’ | Blue |
‘m’ | Magenta |
'and' | yellow |
‘k’ | black |
‘w’ | white |
Matplotlib color display
`python
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.colors as colors
import math
fig = plt.figure()
ax = fig.add_subplot(111)
ratio = 1.0 / 3.0
count = math.ceil(math.sqrt(len(colors.cnames)))
x_count = count * ratio
y_count = count / ratio
x = 0
y = 0
w = 1 / x_count
h = 1 / y_count
for c in colors.cnames:
pos = (x / x_count, y / y_count)
ax.add_patch(patches.Rectangle(pos, w, h, color=c))
ax.annotate(c, xy=pos)
if y >= y_count-1:
x += 1
y = 0
else:
y += 1
plt.show()
For details, refer to the reference materials on the official website of Matplotlib