Python中subplot()和add_subplot()函数

subplot()方法残生的坐标系是按照网格方式划分的,整个画布划分成等分布局的m*n(行*列)的矩阵区域,并按照先行后列的方式对每个区域进行编号,编号从1开始。不能随意划分位置和尺寸大小。

subplot(nrows, ncols, index, projection, polar,             
        sharex,sharey,label, **kwargs)
nrows:表示规划区域的行数。
ncols:表示规划区域的列数。
index:表示选择区域的索引,默认从1开始编号。
projection:表示子图的投影类型。
polar:表示是否使用极坐标,默认值为False。若参数polar设为True,projection='polar'。
sharex:是否共享x轴,默认值为False
sharey:是否共享y轴,默认值为False
注意事项:
1、参数nrows、ncols、index既支持单独传参,也支持以一个3位整数(每位整数必须小于10)的形式传参。
2、subplot()函数会返回一个 Axes类的子类SubplotBase的对象。
3、Figure类对象可以使用add_subplot()方法绘制单子图,此方式与subplot()函数的作用是类似的,但是不等价。
代码示例:
利用折线图展示某一周每天三餐消费数据,数据分别采取利用随机数生成及从数据库读取两种方式,数据应取值合理,要求至少选用两种不同线型、颜色及标记设置分别实现。
%matplotlib auto
import pandas as pd
import pymysql
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
#从数据库获取方式
# 连接数据库
con = pymysql.connect(host='127.0.0.1',user= 'root',password= '123456', database='keshihua')
# 写sql 语句
sql = 'select * from eatmoney'
# 读出数据
df = pd.read_sql(sql, con)
breakfast = np.array(df["早餐"])
lunch = np.array(df["午餐"])
dinner = np.array(df["晚餐"])
# x = np.arange(7)
day=["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]
ax1=plt.subplot(121)
ax1.plot(day,breakfast,marker="o",linestyle="--",color="y",label="早餐")
ax1.plot(day,lunch,marker="*",linestyle="-.",color="b",label="午餐")
ax1.plot(day,dinner,marker=">",color="g",label="晚餐")
ax1.set_title("我的三餐费用")
ax1.legend(loc="best")
ax1.set_xlabel("日期")
ax1.set_ylabel("元")
# plt.text(a,b,b)数据显示的横坐标、显示的位置高度、显示的数据值的大小
for ab1 in zip(day, breakfast):
    ax1.annotate("%s" % ab1[1],xy=ab1,xytext=(-5,5),textcoords='offset points')
for ab2 in zip(day, lunch):
    ax1.annotate("%s" % ab2[1],xy=ab2,xytext=(-5,5),textcoords='offset points')
for ab3 in zip(day, dinner):
    ax1.annotate("%s" % ab3[1],xy=ab3,xytext=(-5,5),textcoords='offset points')
#随机数获取方式
day=["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]
money1 = np.array(np.random.uniform(low=5, high=10, size=7))
money2 = np.array(np.random.uniform(low=5, high=10, size=7))
money3 = np.array(np.random.uniform(low=5, high=10, size=7))
ax2=plt.subplot(122)
ax2.plot(day,breakfast,marker="1",linestyle=":",color="r",label="早餐")
ax2.plot(day,lunch,marker="+",linestyle="--",color="y",label="午餐")
ax2.plot(day,dinner,marker="o",color="b",label="晚餐")
ax2.set_title("我的三餐费用")
ax2.legend(loc="best")
ax2.set_xlabel("日期")
ax2.set_ylabel("元")
# plt.text(a,b,b)数据显示的横坐标、显示的位置高度、显示的数据值的大小
for ab1 in zip(day, breakfast):
    ax2.annotate("%s" % ab1[1],xy=ab1,xytext=(-5,5),textcoords='offset points')
for ab2 in zip(day, lunch):
    ax2.annotate("%s" % ab2[1],xy=ab2,xytext=(-5,5),textcoords='offset points')
for ab3 in zip(day, dinner):
    ax2.annotate("%s" % ab3[1],xy=ab3,xytext=(-5,5),textcoords='offset points')

运行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值