这篇文章主要介绍了python matplotlib折线图样式实现过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
一:简单的折线图
import matplotlib.pyplot as plt#支持中文显示plt.rcParams["font.sans-serif"]=["SimHei"]#x,y数据x_data = [1,2,3,4,5]y_data = [10,30,20,25,28]plt.plot(x_data,y_data)plt.title("简单的折线图")plt.xlabel("x")plt.ylabel("y")plt.show()二、多折线折线图
import matplotlib.pyplot as plt#x,y数据x_data = [1,2,3,4,5]y_data = [10,30,20,25,28]y_data_1 = [12,32,22,27,30]y_data_2 = [8,28,18,23,25]plt.plot(x_data,y_data,x_data,y_data_1,x_data,y_data_2)"""plt.plot(x_data,y_data,x_data,y_data_1)此行可替代为plt.plot(x_data,y_data)plt.plot(x_data,y_data_1)plt.plot(x_data,y_data_2)"""plt.show()三、折线样式:折线颜色、折线图案 、折线宽度
import matplotlib.pyplot as plt#x,y数据x_data = [1,2,3,4,5]y_data = [10,30,20,25,28]y_data_1 = [12,32,22,27,30]plt.plot(x_data,y_data,color="red",linewidth=2.0,linestyle="--")plt.plot(x_data,y_data_1,color="blue",linewidth=2.0,linestyle="-.")plt.show()注:
①color参数:
- 颜色名称或简写#rrggbb
- b: blue
- g: green
- r: red
- c: cyan
- m: magenta
- y: yellow
- k: black
- w: white
- #rrggbb
- (r, g, b) 或 (r, g, b, a),其中 r g b a 取均为[0, 1]之间
- [0, 1]之间的浮点数的字符串形式,表示灰度值。0表示黑色,1表示白色
②linestyle参数
-:代表实线,这是默认值;
--:代表虚线;
·:代表点钱;
-.:代表短线、点相间的虚钱
四、折线图的注解
import numpy as npimport matplotlib.pyplot as plt#x,y数据x_data = np.linspace(0, 2 * np.pi, 100)y_data, y2_data = np.sin(x_data), np.cos(x_data)plt.plot(x_data,y_data,label="y=sinx")plt.plot(x_data,y2_data,label="y=cosx")plt.legend()plt.show()以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。