python之matplotlib1

时间:2020-08-03 19:55:47   收藏:0   阅读:93

首先导入模板matplotlib并指名为plt,以免反复输入pyplot,pyplot包含了很多生成图表的函数,我们创造一和列表,里边保存了前述平方数,再把这个列表传递给plot(),使用plt.show()打开matplotlib的查看器,从而可以显示我们的绘图:

import matplotlib.pyplot as plt
squares = [1,4,9,16,25]
plt.plot(squares)
plt.show()

我们可以调整图像上的标签字体大小和线条粗铣 

import matplotlib.pyplot as plt
squares = [1,4,9,16,25]

plt.plot(squares,linewidth = 5)#设置线条粗细是5
plt.title("Squares Values",fontsize = 24)#设置图标标题
plt.xlabel(Value,fontsize = 15)#设置x轴信息提示,字体为15大小
plt.ylabel(Squares,fontsize = 15)#设置y轴信息提示,字体为15大小
plt.tick_params(axis=both,labelsize = 14)#设置刻度的样式,第一个参数会影响x和y轴的刻度

plt.show()

有时候编译器会给出初始假设值=0,使得我们的绘图从0开始,使得4的平方等于25这种错误现象,我们可以这么做

import matplotlib.pyplot as plt
input_value = [1,2,3,4,5]
squares = [1,4,9,16,25]
plt.plot(input_value,squares,linewidth = 4)
plt.title("square values",fontsize = 14)
plt.xlabel(values,fontsize = 10)
plt.ylabel(squares,fontsize = 10)
plt.tick_params(axis=both,labelsize = 14)

plt.show()

我们可以绘制一个离散的点,其中向scatter(x,y,s = 200)传递两个参数,x表示x坐标,y表示y坐标,s=给出这个点的大小尺寸

import matplotlib.pyplot as plt
plt.scatter(2,4,s = 200)

#设置标题,xy轴的标签显示
plt.title(point,fontsize = 14)
plt.xlabel(x,fontsize = 10)
plt.ylabel(y,fontsize = 10)
plt.tick_params(axis=both,labelsize = 14)

plt.show()

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!