DataFrame.plot函数详解(三)

本文详细介绍了pandasDataFrame的plot函数中bar和barh函数的使用,包括参数rot(旋转角度)、alpha(透明度)、stacked(堆积模式),以及如何创建不同类型的柱状图和设置颜色。
摘要由CSDN通过智能技术生成

DataFrame.plot函数详解(三)

第三部分主要介绍df.bar和df.barh两个函数的使用,还有rot、alpha、stacked三个参数的效果。

1. bar

speed = [0.1, 17.5, 40, 48, 52, 69, 88]
lifespan = [2, 8, 70, 1.5, 25, 12, 28]
index = ['snail', 'pig', 'elephant',
         'rabbit', 'giraffe', 'coyote', 'horse']
df = pd.DataFrame({'speed': speed,
                   'lifespan': lifespan}, index=index)
df.plot.bar(rot=-30,alpha=0.5)  
plt.show()

rot=-30 X轴标签向右旋转30度
alpha=0.5 透明度50%

效果如下:
在这里插入图片描述

speed = [0.1, 17.5, 40, 48, 52, 69, 88]
lifespan = [2, 8, 70, 1.5, 25, 12, 28]
index = ['snail', 'pig', 'elephant',
         'rabbit', 'giraffe', 'coyote', 'horse']
df = pd.DataFrame({'speed': speed,
                   'lifespan': lifespan}, index=index)
df.plot.bar(rot=-30,alpha=0.5,stacked=True)  
plt.show()

stacked=True ,柱状图堆积显示

在这里插入图片描述

df = pd.DataFrame(abs(np.random.randn(8,2)), columns=['A','B'])
axes = df.plot.bar(rot=0, subplots=True, alpha=0.6)
axes[0].legend(loc='upper left')  
axes[1].legend(loc='upper right')  
plt.show()

axes[0].legend df.plot返回值是axes对象,分别设置对象的图示位置

在这里插入图片描述

2.barh

index = ['snail', 'pig', 'elephant','rabbit', 'giraffe', 'coyote', 'horse', 'cock']

df = pd.DataFrame(abs(np.random.randn(8,2)), columns=['A','B'],index=index)
ax = df.plot.barh(alpha=0.6)

简单的水平柱状图
在这里插入图片描述

ax = df.plot.barh(alpha=0.6,stacked=True,color={"A": "red", "B": "green"})

color={“A”: “red”, “B”: “green”} 分别设置两列数据不同颜色

水平堆积柱状图
在这里插入图片描述
上一篇Line函数,及主要参数style 、marker、color、linewidth、markersize 、grid、xlim、ylim 、 loc 、subplot使用
下一篇area 、pie 、hist 、hexbin函数演示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值