python_matplotlib DAY_21(1)

学习内容
matplotlib的使用,虽然之前的内容以及学习过数据可视化的内容,
但是还是要系统学习matplotlib在pycharm里面的使用
重点
1.散点图

import matplotlib.pyplot as plt#使用前插入matplotlib
plt.scatter(x=,y=,s=,c='',marker='',alpha=)
#散点图必须要有x,y轴,s代表面积,可以自己设置,c代表颜色,marker是显示的图标,alpha代表透明度
plt.show()#显示图像指令

2.折线图

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

a = np.linspace(-np.pi, np.pi, 1000)
b = np.sin(a)
plt.plot(a, b)
plt.show()#numpy自带sin和Π的数据

在这里插入图片描述
3.条形图

a=[5,10,15,20]
b=[7,13,18,22]
c=np.arange(4)
bar_width=0.3
plt.bar(c+bar_width,height=a,width=bar_width,color='r')
plt.bar(c,height=b,width=bar_width,color='b')

plt.show()#两幅图一起画

在这里插入图片描述

a=[5,10,15,20]
b=[7,13,18,22]
c=np.arange(4)
bar_width=0.3
plt.bar(c,height=a,width=bar_width,bottom=b,color='r')
plt.bar(c,height=b,width=bar_width,color='b')

plt.show()#重叠画

在这里插入图片描述

4.直方图
区别条形图

mn=10#均值
sigma=20#方差
x=mn+sigma*np.random.rand(1000)
plt.hist(x,bins=100,color='r')
plt.show()

在这里插入图片描述

#二维hist2d
mn=10
sigma=20
x=mn+sigma*np.random.randn(1000)
y=2*mn+0.8*sigma*np.random.randn(1000)
plt.hist2d(x,y,bins=100)
plt.show()

在这里插入图片描述

5.饼状图

label = ['A', 'B', 'C', 'D']
num = [10, 20, 30, 40]
plt.axes(aspect=1)  # 设置成正圆
explode = [0.1, 0.1, 0.1, 0.1]#设置远离圆心的距离
plt.pie(x=num, labels=label, autopect='%0.2f%%'explode=explode, shadow=True)
#设置阴影和距离,这里%0.2意味小数点后面两位,%%代表显示百分号
plt.show()

在这里插入图片描述

6.箱线图

data = np.random.normal(size=(1000, 4), loc=0, scale=1)
labels = ['a', 'b', 'c', 'd']
plt.boxplot(data,sym='X',whis=0.5,labels=labels)#错误值用X,上下分为距离为whis
plt.show()

在这里插入图片描述

发布了41 篇原创文章 · 获赞 1 · 访问量 926

猜你喜欢

转载自blog.csdn.net/soulproficiency/article/details/104105355
今日推荐