一、numpy数组
可以使用numpy的数组作为画图的数据
t = np.arange(0,5,1)
plt.plot(t,t**2)
plt.show()
arange 函数:可以用于生成一堆数组,使用频率非常的高
语法:
numpy.arange(start, stop, step, dtype = None)
参数:
Start:开始位置,数字,可选项,默认起始值为0
stop:停止位置,数字
step:步长,数字,可选项, 默认步长为1,如果指定了step,则还必须给出start。
dtype :输出数组的类型。 如果未给出dtype,则从其他输入参数推断数据类型。
二、matplotlib画图
发现: matplotlib单个画图,也支持在一个图上画多组数据。
目前的待探究的问题:
但是不支持中文的坐标,可以进行替换。还有一些数据
还有不同的颜色可以进行设置.
plt.plot([1,3,5,7],[2,4,6,8])
plt.plot([1,2,3,4],[1,3,5,8])
plt.ylabel("some number in y")#y轴的名称
plt.xlabel("some number in x")#x轴的名称
plt.show()
三、各种图形尝试
1. 柱状图
使用bar 方法 可以任意改变y轴的长度,
bar:(names,values)
names = ['2016','2017','2018','2019']
values = [1,10,100,1000]
plt.bar(names,values)
plt.show()
效果图
image.png
2.饼图
labels = "cat","dog","baby",'house' #标签
sizes = [15,30,45,10]#产生锲形块的数据
#产生锲形块分离距离序列,0.1代表的是其中一个距离其他的有多远
explode = (0,0.1,0,0)
fig1,ax1 = plt.subplots()
autopct = '%1.1f%%' #锲形块的数据标注格式
#绘制饼图
ax1.pie(sizes,explode = explode,
labels = labels,autopct = autopct,shadow = True,startangle = 90)
plt.show()
image.png
3. 散点图
names = ['2016','2017','2018','2019']
values = [1,10,100,1000]
plt.scatter(names,values)
plt.show()
image.png
4. 绘制多个子图
可以实现将多个图同时放在一张画布上。
plt.figure(1) # the first figure
plt.subplot(211) # the first subplot in the first figure
plt.plot([1, 2, 3])
plt.subplot(212) # the second subplot in the first figure
plt.plot([4, 5, 6])
image.png