数据可视化(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()
相关推荐
songyuc9 小时前
Matplotlib&seaborn学习笔记
笔记·学习·matplotlib
威尔逊·柏斯科·希伯理13 小时前
机器学习第一天(共12天)
人工智能·python·机器学习·conda·numpy·pandas·matplotlib
Cloud_Shy61810 天前
Python 数据分析基础入门:《Excel Python:飞速搞定数据分析与处理》学习笔记系列(第十一章 Python 包跟踪器 上篇)
python·数据分析·excel·pandas·matplotlib
键盘上的猫头鹰11 天前
Matplotlib可视化教程:从入门到精通
数据分析·matplotlib
深兰科技12 天前
深兰科技签约乌兹别克斯坦智慧城市项目,推动中国AI出海规模化
人工智能·beautifulsoup·numpy·智慧城市·fastapi·matplotlib·深兰科技
byzh_rc1 个月前
[AI工具从入门到入土] 命令行
网络·人工智能·python·深度学习·matplotlib
小何code1 个月前
人工智能【第7篇】数据可视化:Matplotlib与Seaborn实战(万字长文+完整代码)
人工智能·机器学习·信息可视化·matplotlib
qq_283720051 个月前
Python3 模块精讲:Matplotlib—— 数据可视化、绘图从零基础到实战精通
信息可视化·matplotlib
2401_827499991 个月前
数据分析学习06(黑马)-Matplotlib
学习·数据分析·matplotlib
哈伦20191 个月前
第六章 Matplotlib案例股票K线图绘制
python·matplotlib