python数据可视化实验:坐标轴共享及子图布局

 1. 把包括本人在内的至少五位同学的身高存储在csv文件中,选择合适图表进行绘制,要求利用坐标轴共享分别采用厘米和米作为度量单位。

%matplotlib auto
import numpy as np 
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
arr=np.loadtxt("F:/DaiMa/MyJupyter/data/height.csv", dtype=str,delimiter=",",encoding='utf-8')
x = np.arange(5)
y = [int(arr[0][1]),int(arr[1][1]),int(arr[2][1]),int(arr[3][1]),int(arr[4][1])]
name = [arr[0][0],arr[1][0],arr[2][0],arr[3][0],arr[4][0]]
#print(y,"\n",name)
fig,ax=plt.subplots()
ax_left=ax.bar(x,y,width=0.3)
ax.set_xlabel("姓名",loc="right")
ax.set_ylabel("身高(cm)",loc="top")
ax.set_xticks([0,1,2,3,4])
ax.set_xticklabels(name)
ax.set_ylim(155,190)
#共享坐标轴
ax_right = ax.twinx()
y2=[y[0]/10,y[1]/10,y[2]/10,y[3]/10,y[4]/10]
line = ax_right.plot(x,y2,c="r",marker="o")
ax_right.set_ylim(15.5,19.0)
ax_right.set_ylabel("身高(m)",loc="top")
# 添加图例
plt.legend([ax_left,line[0]], ['身高(cm)', '身高(m)'])
for a,b in zip(x,y):
    ax.text(a,b+1,b,ha='center', va='bottom')
plt.show()

运行结果:

2. 利用坐标轴共享在同一子图中绘制2022年洛阳市区每月的最高气温、最低气温和累计降水量,数据可以从网上爬取、存储在数据库中、存储在文件中、也可用随机数生成

%matplotlib auto
import numpy as np 
import matplotlib as mpl
import matplotlib.pyplot as plt
#最高气温
tmax=np.array(np.random.uniform(20,30,12))
#最低气温
tmin=np.array(np.random.uniform(0,10,12))
#累计降水量
rain=np.array(np.random.uniform(50,300,12))
x = np.arange(12)
print(tmax,"\n",tmin,"\n",rain)
fig,ax = plt.subplots()
ax.set_ylim(0,40)
barmin=ax.bar(x,tmin,color="yellow")
barmax=ax.bar(x,tmax,bottom=tmin,color="orange")
ax.set_xticks([0,1,2,3,4,5,6,7,8,9,10,11])
ax.set_xticklabels(['1月', '2月', '3月', '4月', '5月', '6月',
                    '7月', '8月', '9月', '10月', '11月', '12月'])
ax.set_xlabel("月份")
ax.set_ylabel("温度(℃)")
ax_right = ax.twinx()
plotrain = ax_right.plot(x,rain,'o-m')
ax_right.set_ylabel("降水量(ml)")
# 添加图例
plt.legend([barmin,barmax,plotrain[0]], ['最低温', '最高温','累计降水量'],loc="best")
plt.show()

3. 自选2个包含多个子图的实验项目,分别利用约束布局、紧密布局和自定义布局方式,对比三种布局方式。

#约束布局
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
fig,axs = plt.subplots(2,2,constrained_layout=True)
fig.suptitle("约束布局")
axs[0,0].plot([1,2,3],[4,5,6])
axs[0,1].plot([1,2,3],[4,5,6])
axs[1,0].plot([1,2,3],[4,5,6])
axs[1,1].plot([1,2,3],[4,5,6])
plt.show()

#紧密布局
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
fig,axs = plt.subplots(2,2)
fig.suptitle("紧密布局")
axs[0,0].plot([1,2,3],[4,5,6])
axs[0,1].plot([1,2,3],[4,5,6])
axs[1,0].plot([1,2,3],[4,5,6])
axs[1,1].plot([1,2,3],[4,5,6])
#调整子图之间的距离
plt.tight_layout(pad=2,h_pad=1,w_pad=1,rect=(0,0.5,1,1.5))
# pad:表示画布边缘与子图边缘之间的空白区域的大小,默认为1.08。
# h_pad,w_pad:表示相邻子图之间的空白区域的大小。
# rect:表示调整所有子图位置的矩形区域的四元组(left, bottom, right, top),默认为 (0,0,1,1)。


#自定义布局
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
plt.suptitle("自定义布局")
fig2 = plt.figure()
spec2 = gridspec.GridSpec(ncols=2, nrows=2, figure=fig2,wspace=0.2,hspace=0.5)
f2_ax1 = fig2.add_subplot(spec2[0, 0])
f2_ax2 = fig2.add_subplot(spec2[0, 1])
f2_ax3 = fig2.add_subplot(spec2[1, 0])
f2_ax4 = fig2.add_subplot(spec2[1, 1])
plt.show()

运行结果:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值