数据可视化(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()
相关推荐
Serendipity_Carl1 天前
爬虫数据清洗可视化链家房源
python·pandas·matplotlib
合作小小程序员小小店4 天前
基于可视化天气系统demo,基于python+ matplotlib+request爬虫,开发语言python,数据库无,10个可视化界面,需要的可以了联系。
开发语言·爬虫·python·matplotlib
景彡先生4 天前
Python matplotlib详解:从入门到精通,数据可视化利器
python·信息可视化·matplotlib
world-wide-wait6 天前
机器学习03——matplotlib
python·机器学习·matplotlib
~~李木子~~9 天前
Matplotlib 数据可视化基础测试题
信息可视化·matplotlib
MediaTea15 天前
Python 第三方库:matplotlib(科学绘图与数据可视化)
开发语言·python·信息可视化·matplotlib
Kratzdisteln15 天前
【Python】绘制椭圆眼睛跟随鼠标交互算法配图详解
python·数学·numpy·pillow·matplotlib·仿射变换
万粉变现经纪人15 天前
如何解决 pip install -r requirements.txt 子目录可编辑安装缺少 pyproject.toml 问题
开发语言·python·scrapy·beautifulsoup·scikit-learn·matplotlib·pip
jie*20 天前
小杰深度学习(seventeen)——视觉-经典神经网络——MObileNetV3
人工智能·python·深度学习·神经网络·numpy·matplotlib
jie*21 天前
小杰深度学习(sixteen)——视觉-经典神经网络——MobileNetV2
人工智能·python·深度学习·神经网络·tensorflow·numpy·matplotlib