数据可视化(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()
相关推荐
java1234_小锋16 小时前
一周学会Matplotlib3 Python 数据可视化-绘制热力图(Heatmap)
开发语言·python·信息可视化·matplotlib·matplotlib3
java1234_小锋1 天前
一周学会Matplotlib3 Python 数据可视化-绘制散点图(Scatter)
开发语言·python·信息可视化·matplotlib·matplotlib3
姜—姜2 天前
数据分析总结
数据挖掘·数据分析·numpy·pandas·matplotlib·jieba·seaborn
万粉变现经纪人2 天前
何解决PyCharm中pip install安装Python报错ModuleNotFoundError: No module named ‘json’问题
python·pycharm·json·beautifulsoup·scikit-learn·matplotlib·pip
java1234_小锋3 天前
周学会Matplotlib3 Python 数据可视化-绘制折线图(Lines)
开发语言·python·信息可视化·matplotlib·折线图·matplotlib3
java1234_小锋3 天前
一周学会Matplotlib3 Python 数据可视化-绘制直方图(Histogram)
开发语言·python·信息可视化·matplotlib·matplotlib3
OAFD.6 天前
Matplotlib 入门到实战:从零开始学 Python 数据可视化
python·信息可视化·matplotlib
咩?7 天前
SEABORN库函数(第十八节课内容总结)
开发语言·python·matplotlib·seaborn
仪器科学与传感技术博士7 天前
Matplotlib库:Python数据可视化的基石,发现它的美
开发语言·人工智能·python·算法·信息可视化·matplotlib·图表可视化
java1234_小锋7 天前
一周学会Matplotlib3 Python 数据可视化-坐标轴 (Axis)
开发语言·python·信息可视化·matplotlib·matplotlib3