python数据可视化实验:subplot2grid子图的绘制

利用excel保存本人连续两周不同消费类别的支出数据,至少选择两种可视化图表展示两周消费数据和不同消费的占比情况,要求利用subplot2grid绘制在一个画布中,各子图有合理标题和坐标轴设置,子图间有合理间隔。

%matplotlib auto
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
df = pd.read_excel("F:/DaiMa/MyJupyter/data/money.xlsx")
#display(df)

ax1 = plt.subplot2grid((2,1),(0,0))
x1=np.arange(8)
y11=df.iloc[0,1:9]
y12=df.iloc[1,1:9]
ax1.set_xticks([0,1,2,3,4,5,6,7])
ax1.set_xticklabels(df.columns[1:9])
ax1.set_xlabel("类型",loc="right")
ax1.set_ylabel("人民币(元)",loc="top")
ax1.plot(x1,y11,label="第一周")
ax1.plot(x1,y12,label="第二周")
ax1.legend(loc="best")
for a,b in zip(x1,y11):
    ax1.text(a,b+0.5,b,ha="center",va="bottom")
for a,b in zip(x1,y12):
    ax1.text(a,b+0.5,b,ha="center",va="bottom")
    
    
    
ax2 = plt.subplot2grid((2,2),(1,0))
ax2.set_title("%ddir一周消费")
x2=df.iloc[0,1:9]
y2=df.columns[1:9]
# 饼图空隙
dev_position = [0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05]
ax2.pie(x2,autopct='%3.1f%%', explode=dev_position,labels=y2)


ax3 = plt.subplot2grid((2,2),(1,1))
ax3.set_title("第二周消费")
x3=df.iloc[1,1:9]
y3=df.columns[1:9]
# 饼图空隙
dev_position = [0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05]
ax3.pie(x3,autopct='%3.1f%%', explode=dev_position,labels=y3)


#调整子图间距
plt.tight_layout()
plt.show()

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值