数据可视化(2)

1.柱状图

python 复制代码
#柱状图
#bar(x,height,width,*,align='center',**kwargs)
#height柱子的高度,即y轴上的数据
#width数组的宽度,默认值0.8
#*表示后面的参数为匿名关键字,必须传入参数
#kwargs关键字参数

x=[1,2,3,4,5]
height=[random.randint(10,100)for i in range(5)]
plt.bar(x,height)
plt.show()


df=pd.read_excel("产品销售.xlsx")
x=df['产品名称']
height=df['总量']
plt.figure(10,6)
plt.bar(x,height,width=0.5,alpha=0.5)
plt.grid(axis='y',linestyle='--')
plt.xlabel("产品名称")
plt.yticks("销量")
plt.title('产品销售量',fontsize=18)
#设置图例
plt.legend(('销售额',))
#设置文本标签
#alpha=0.9设置透明度
for a,b in zip(x,height):
    plt.text(a,b,format(b,','),ha='center',va='center',fontsize=12,color='b',alpha=0.9)
plt.show()

2.多柱状图

python 复制代码
df=pd.read_excel("产品销售.xlsx")
plt.figure(10,6)
#x=df['产品名称']
x=np.array([0,1,2,3,4,5,6,7])
y1=df['1月']+df['2月']+df['3月']
y2=df['4月']+df['5月']+df['6月']
y3=df['7月']+df['8月']+df['9月']
y4=df['10月']+df['11月']+df['12月']
bar_width=0.2#设置柱子的宽度
plt.ylabel("季度销售")
plt.xlabel("产品名称")
plt.title("季度销售量")
plt.bar(x,y1,bar_width,color='c',alpha=0.5)
plt.bar(x+bar_width,y2,bar_width,color='b',alpha=0.5)
plt.bar(x+2*bar_width,y3,bar_width,color='y',alpha=0.5)
plt.bar(x+3*bar_width,y4,bar_width,color='r',alpha=0.5)
#设置坐标轴刻度
data=df['产品名称']
plt.xticks(x,data)
#添加文本标签
for a,b in zip(x,y1):
    plt.text(a,b,format(b,','),ha='center',va='bottom',fontsize=8)
for a,b in zip(x,y2):
    plt.text(a+bar_width,b,format(b,','),ha='center',va='bottom',fontsize=8)
for a,b in zip(x,y3):
    plt.text(a+2*bar_width,b,format(b,','),ha='center',va='bottom',fontsize=8)
for a,b in zip(x,y4):
    plt.text(a+3*bar_width,b,format(b,','),ha='center',va='bottom',fontsize=8)
#设置图例
plt.legend(['第一季度','第二季度','第三季度','第四季度'])
plt.show()

3.基本直方图

python 复制代码
#直方图
#plt.hist(x,bins)
#bins:统计数据的区间分布

x=[2,34,52,62,12,35,45,88,26,13,16]
bins=[0,25,50,75,100]
plt.hist(x,bins)
plt.show()
python 复制代码
#使用直方图分析成绩分布情况
df=pd.read_excel('成绩表.xlsx')
#解决中文乱码
plt.rcParams['font.sans-serif']=['SimHei']

x=df['总成绩']
#设置坐标轴标题
plt.xlabel('分数')
plt.ylabel('学生姓名')
#设置图表的标题
plt.title('成绩分布直方图',fontsize=18)
#设置数据的区间
bins=[40,50,60,70,80,90,100]
plt.hist(x,bins,facecolor='b',edgecolor='k')
plt.show()
相关推荐
ZPC821010 小时前
Python使用matplotlib绘制图形大全(曲线图、条形图、饼图等)
开发语言·python·matplotlib
IT_Beijing_BIT7 天前
Python库matplotlib之二
开发语言·python·matplotlib
跟德姆(dom)一起学AI7 天前
0基础跟德姆(dom)一起学AI 数据处理和统计分析08-日期类型处理,Matplotlib介绍
开发语言·人工智能·python·信息可视化·numpy·matplotlib
AIAdvocate10 天前
Matplotlib-数据可视化详解
python·信息可视化·matplotlib
猫天意10 天前
Matplotlib | 一文搞定Matplotlib从入门到实战演练!
数据挖掘·数据分析·numpy·matplotlib
(lemon seed)11 天前
Matplotlib绘图基础
信息可视化·matplotlib
浊酒南街12 天前
plt常用函数介绍一
python·机器学习·matplotlib
开出南方的花12 天前
数据处理与统计分析篇-day10-Matplotlib数据可视化
jupyter·信息可视化·plotly·numpy·pandas·matplotlib·ipython
AIAdvocate12 天前
Pandas-日期类型处理代码详解
python·信息可视化·pandas·matplotlib
白鸟无言15 天前
照片EXIF数据统计与可视化
python·matplotlib