问题相关代码,请勿粘贴截图
from pandas.plotting import register_matplotlib_converters
import pandas as pd
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import xlwt
import requests
import re
import json
import platform
head={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.53'}
req=requests.get('http://money.finance.sina.com.cn/forex/api/jsonp.php/SINAREMOTECALLCALLBACK.CALLBACK_1637050185601883563512783069/FusionChart_Service.getForexChartInfo?country=%D6%D0%B9%FA&event=%B2%C6%D0%C2%D7%DB%BA%CFPMI&datefrom=2017-03-03&dateto=2021-11-0-3&allinfo=1&npp=20&page=1'
,headers=head)
freq=requests.get('http://money.finance.sina.com.cn/forex/api/jsonp.php/SINAREMOTECALLCALLBACK.CALLBACK_163737461216346743293146016707/FusionChart_Service.getForexChartInfo?country=%D6%D0%B9%FA&event=%B2%C6%D0%C2%B7%FE%CE%F1%D2%B5PMI&datefrom=2017-03-03&dateto=2021-11-0-3&allinfo=1&npp=20&page=1'
,headers=head)
zreq=requests.get('http://money.finance.sina.com.cn/forex/api/jsonp.php/SINAREMOTECALLCALLBACK.CALLBACK_1637374612163737673381537736465096858196/FusionChart_Service.getForexChartInfo?country=%D6%D0%B9%FA&event=%B2%C6%D0%C2%D6%C6%D4%EC%D2%B5PMI&datefrom=2017-03-03&dateto=2021-11-0-3&allinfo=1&npp=20&page=1',
headers=head)
req=req.text
freq=freq.text
zreq=zreq.text
x=req[118:2642]
f=freq[120:2700]
z=zreq[130:2712]
#print(f)
#print(x)
#print(z)
listx=json.loads(x)
listf=json.loads(f)
listz=json.loads(z)
#print(listx)
x_data=[]
y_data=[]
fx_data=[]
fy_data=[]
zx_data=[]
zy_data=[]
for i in listx:
riqi=i['date']
shuzhi=i['ifr_actual']
#print(riqi,shuzhi)
x_data.append(riqi)
y_data.append(shuzhi)
for o in listf:
friqi=o['date']
fshuzi=o['ifr_actual']
#print(friqi,fshuzi)
fx_data.append(friqi)
fy_data.append(fshuzi)
for t in listz:
zriqi=t['date']
zshuzhi=t['ifr_actual']
#print(zriqi,zshuzhi)
zx_data.append(zriqi)
zy_data.append(zshuzhi)
# 为日期转换做准备
register_matplotlib_converters()
# 读取数据 修改字段名,选择6列数据
# 准备画布
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# 获取日期列表
x = pd.to_datetime(x_data)
y = pd.to_datetime(fx_data)
z = pd.to_datetime(zx_data)
# 画折线
ax.plot(x, y_data, color='tab:blue')
ax.plot(y, fy_data, color='tab:orange')
ax.plot(z, zy_data, color='tab:green')
# 设置月定位符 , 按季度显示,否则太挤了
ax.xaxis.set_major_locator(mdates.MonthLocator(interval=3)) # interval = 3 季度
# 设置日期的格式
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y%m'))
# 显示
plt.show()
我的解答思路和尝试过的方法
我原本是打算用ax.yaxis.set_major_locator但是效果并不理想。
我想要达到的结果
让它看起来正常点,能看,能用就好了。