数据可视化(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()
相关推荐
爱喝可乐的老王2 天前
数据分析实践--数据解析购房关键
信息可视化·数据分析·pandas·matplotlib
海棠AI实验室2 天前
第 十五 章 可视化入门:Matplotlib 做出像样的图
matplotlib
渡我白衣8 天前
计算机组成原理(11):加法器
python·机器学习·numpy·pandas·matplotlib·计组·数电
老歌老听老掉牙8 天前
使用 Matplotlib 自定义坐标轴字体及刻度样式详解
python·matplotlib
AC赳赳老秦10 天前
DeepSeek教育科技应用:智能生成个性化学习规划与知识点拆解教程
前端·网络·数据库·人工智能·学习·matplotlib·deepseek
AC赳赳老秦12 天前
行业数据 benchmark 对比:DeepSeek上传数据生成竞品差距分析报告
开发语言·网络·人工智能·python·matplotlib·涛思数据·deepseek
2401_8414956413 天前
【Python高级编程】图着色动态可视化 APP
python·算法·matplotlib·tkinter·回溯法·图着色算法·动态可视化工具
laocooon52385788613 天前
对传入的 x , y 两个数组做折线图, x 对应 x 轴, y 对应 y 轴。并保存到 Task1/image1/T2.png
python·numpy·pandas·matplotlib
渡我白衣14 天前
Python 与数据科学工具链入门:NumPy、Pandas、Matplotlib 快速上手
人工智能·python·机器学习·自然语言处理·numpy·pandas·matplotlib
shenzhenNBA17 天前
python模块matplotlib绘图-饼图
python·matplotlib·pyplot·python绘制图表