python——plt.figure()画子图(双轴图)双Y轴实例

话对比图,如果两个数量级的纵坐标,我们要进行趋势比较,放在同一坐标轴,某一个往往被压缩的的很小。所以需要左右双Y轴画图。

1.随便获取数列,对应的索引化成一样就可以了,下面是量化的数据处理,可以不用看,只要能化成这里的输出类型就可以了。

# coding=utf-8
import math
import tushare as ts
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import talib
import pandas as pd
from datetime import datetime, date
matplotlib.rcParams['axes.unicode_minus']=False
plt.rcParams['font.sans-serif']=['SimHei']
ts.set_token('19fbtoken码.e0')#需要注册tushar pro 获取token码  [获取地址](#https://tushare.pro/register?reg=385920) 
pro = ts.pro_api()
#读取数据
star='2010Q1'
end='2020Q1'
start_cpi='200601'
end_cpi='202003'
start_time='20060301'
end_time="20200531"
dcc= pro.cn_cpi(start_m=start_cpi, end_m=end_cpi)#,fields='nt_yoy')
df1 = pro.cn_gdp(start_q=star, end_q=end)
dsc1 = pro.index_daily(ts_code='000300.SH', start_date=start_time, end_date=end_time,fields='ts_code,trade_date,close')
dsp = pro.index_daily(ts_code='NHCI.NH', start_date=start_time, end_date=end_time,fields='ts_code,trade_date,close')
dsb = pro.index_daily(ts_code='000012.SH', start_date=start_time, end_date=end_time,fields='ts_code,trade_date,close')
def mon_fun(dsb):
    dsb.index = pd.to_datetime(dsb.trade_date,format="%Y-%m")#转换成datetime类型的
    dsbb=dsb.resample("M").mean()
    return dsbb
dsbb=mon_fun(dsb)
dscc1=mon_fun(dsc1)
dspp=mon_fun(dsp)
dsbb.rename(columns={"close":"close_b"},inplace=True)
dscc1.rename(columns={"close":"close_c"},inplace=True)
dspp.rename(columns={'close':'close_p'},inplace=True)
dcc.index=pd.to_datetime(dsbb.index,format="%Y-%m")

输出同索引的DataFrame

在这里插入图片描述
2.设置图的尺寸、还有画上证国债指数和CPI同比变化的对比图(主图)

fig = plt.figure(figsize=(10,6))
ax1 = fig.add_subplot(111)
ax1.plot(dscc1.close_c,)
ax1.set_ylabel('上债综合指数闭盘价',fontdict={'weight': 'normal', 'size': 15})
ax1.set_title("上债综合指数与CPI同比变化对比图",fontdict={'weight': 'normal', 'size': 15})

3.画子图

ax2 = ax1.twinx()  # this is the important function
ax2.plot( dcc.nt_yoy , 'r')
ax2.set_ylabel('CPI全国同比',fontdict={'weight': 'normal', 'size': 15})
ax2.set_xlabel('Same')
plt.show()

4.效果
在这里插入图片描述

5.在比如画南华商品指数与CPI的变化对比,只需改动坐标ax1

################数据x坐标轴需要两个相同。共享的X轴###################
fig = plt.figure(figsize=(10,6))
ax1 = fig.add_subplot(111)
ax1.plot(dspp.close_p,)
ax1.set_ylabel('南华指数闭盘价',fontdict={'weight': 'normal', 'size': 15})
ax1.set_title("南华指数与CPI同比变化对比图",fontdict={'weight': 'normal', 'size': 15})

ax2 = ax1.twinx()  # this is the important function
ax2.plot( dcc.nt_yoy , 'r')
ax2.set_ylabel('CPI全国同比',fontdict={'weight': 'normal', 'size': 15})
ax2.set_xlabel('Same')
#参数rotation设置坐标旋转角度,参数fontdict设置字体和字体大小
#ax1.set_xticklabels(rotation=90,fontdict={'weight': 'normal', 'size': 15})
plt.show()

5.结果
在这里插入图片描述

其他量化策略代码,获取token 后可以直接用
1.python量化——alpha股票-指数期货对冲策略
2.多因子选股策略
3.海龟交易策略
4.移动平均策略——单/双均线策略
5.改进的美林时钟策略(一)
5.改进的美林时钟策略(二)
6.改进的美林时钟策略(三)

  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 在Python中,使用plt.plot可以多个。可以通过以下步骤实现: 1. 导入matplotlib.pyplot模块:import matplotlib.pyplot as plt 2. 创建一个figure对象:fig = plt.figure() 3. 创建多个子图:ax1 = fig.add_subplot(2, 1, 1)和ax2 = fig.add_subplot(2, 1, 2) 4. 在每个子图中使用plt.plot:ax1.plot(x1, y1)和ax2.plot(x2, y2) 5. 可以使用plt.show()显示所有子图。 例如,以下代码可以出两个子图,分别显示sin(x)和cos(x)的像: import numpy as np import matplotlib.pyplot as plt x = np.linspace(, 2*np.pi, 100) y1 = np.sin(x) y2 = np.cos(x) fig = plt.figure() ax1 = fig.add_subplot(2, 1, 1) ax2 = fig.add_subplot(2, 1, 2) ax1.plot(x, y1) ax2.plot(x, y2) plt.show() ### 回答2: 在Python中,Matplotlib是一个非常常用的可视化库。它可以让我们使用plt.plot出折线、散点等等,同时也可以多个。 要在plt.plot中多个,我们可以使用subplot()函数来创建多个像子区域。subplot()函数接受三个整数参数,表示将窗分成多少行、多少列以及在哪个子区域上进行绘。例如: plt.subplot(2, 1, 1) # 将窗分为2行1列,选择第1个子区域进行 plt.plot(x1, y1) plt.subplot(2, 1, 2) # 将窗分为2行1列,选择第2个子区域进行 plt.plot(x2, y2) 上述代码就创建了一个2行1列的窗,并在第1个子区域中出了x1和y1的折线,同时在第2个子区域中出了x2和y2的折线。 此外,我们还可以使用figure()函数来创建新的窗,这样我们就可以在同一个Python脚本中出多个。例如: plt.figure(1) # 创建编号为1的plt.subplot(2, 1, 1) # 在第1个子区域 plt.plot(x1, y1) plt.subplot(2, 1, 2) # 在第2个子区域 plt.plot(x2, y2) plt.figure(2) # 创建编号为2的plt.subplot(2, 1, 1) # 在第1个子区域 plt.plot(x3, y3) plt.subplot(2, 1, 2) # 在第2个子区域 plt.plot(x4, y4) 上述代码在两个不同的窗中出了四个不同的折线。 总之,通过使用subplot()函数和figure()函数,我们可以轻松地在Python出多个像,进行有效的数据可视化和分析。 ### 回答3: 在Python的Matplotlib库中,我们可以使用plt.plot()函数绘制一个形。使用plt.plot()函数时,可以同时绘制多个形,这有助于我们在同一张表中比较多个数据集,并且使得表更加清晰和易于理解。 首先,我们需要导入Matplotlib库以及Numpy库(如果需要用到数据处理部分)。我们可以使用以下代码将它们导入: import matplotlib.pyplot as plt import numpy as np 然后,我们可以使用plt.plot()函数绘制多个形。有多种绘制多个形的方式,其中一个方法是在plt.plot()函数中传递多个x和y轴的数据数组。 例如,我们可以使用以下代码分别绘制三个数据集: x1 = np.linspace(0, 5, 10) y1 = x1 ** 2 x2 = np.linspace(0, 5, 10) y2 = x2 x3 = np.linspace(0, 2 * np.pi, 50) y3 = np.sin(x3) plt.plot(x1, y1, 'r') plt.plot(x2, y2, 'g') plt.plot(x3, y3, 'b') 在上面的代码中,我们分别定义了三个数据集(x1, y1)、(x2, y2)和(x3, y3),然后分别使用plt.plot()函数绘制它们。plt.plot()函数中的第三个参数可以设置线条的颜色,'r'表示红色,'g'表示绿色,'b'表示蓝色。 绘制多个形时,我们也可以使用plt.subplots()函数,默认情况下,plt.subplots()函数会创建一个包含一个子图表。如果我们想要在同一个布中绘制多个子图,可以通过在plt.subplots()函数中指定“row”和“column”的数量来实现。 例如,我们可以使用以下代码在同一个布中绘制三个子图: fig, axs = plt.subplots(nrows=3, ncols=1, figsize=(5,12)) axs[0].plot(x1, y1) axs[1].plot(x2, y2) axs[2].plot(x3, y3) 在上面的代码中,我们创建了一个包含三个子图布,每个子图垂直排列。plt.subplots()函数中的“nrows”和“ncols”参数分别指定了行数和列数, “figsize”参数指定了布的大小。在每个子图上,我们使用.axs[]索引表示需要绘制的形,并传递相应的数据集。 以上就是在Python中使用plt.plot函数绘制多个形的两种方式。您可以根据实际需要选择使用哪种方法,以便更好地比较多个数据集,同时使得表更加清晰易懂。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李、不姓李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值