Matplotlib/Pandas:使用时间序列缩放绘图的一部分

Matplotlib/Pandas: Zoom Part of a Plot with Time Series(Matplotlib/Pandas:使用时间序列缩放绘图的一部分)
本文介绍了Matplotlib/Pandas:使用时间序列缩放绘图的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的任务很简单:我有一个时间序列 ts(2010 年和 2014 年之间的欧元瑞士法郎每日汇率)来绘制.在该图中,我想通过放大某个时间间隔来突出显示它.但是,缩放后的窗口仍然是空的(参见下面的代码).此外,我在选择放大窗口的 x 范围时遇到了问题,因为我不知道如何正确地将日期转换为 matplotlib 的内部整数表示.

my task is simple: I have a time series ts (Euro Swiss Franc daily exchange rates between 2010 and 2014) to plot. In that plot I would like to highlight a certain time interval by zooming into it. However, the zoomed window stays simply empty (see code below). Furthermore, I have a problem by selecting the x-range of the zoomed-in window since I do not know how to properly transform the dates to the internal integer representation of the matplotlib.

提前感谢您的帮助!

这是我的代码:

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes 
from mpl_toolkits.axes_grid1.inset_locator import mark_inset

# Load the time series
ts = pd.read_csv('./Data/EUR_CHF_Exchange_Rates/EUR_CHF_daily.csv',sep=';', parse_dates=['time'], index_col = 'time',decimal=',')
ts = ts['EUR/CHF']
ts = ts.sort_index(ascending=True)

# Plot
fig = plt.figure(figsize=(14,5))
ax = plt.axes()
ts.plot() # ts is my time series

# Label the axis
ax.set_xlabel('')
ax.set_ylabel('EUR/CHF')

#I want to select the x-range for the zoomed region. I have figured it out suitable values
# by trial and error. How can I pass more elegantly the dates as something like
# x1 = '2012-05-09'
# x2 = '2012-09-02'
x1 = 15439.0
x2 = 15588.0

# select y-range for zoomed region
y1 = 1.15
y2 = 1.25

# Make the zoom-in plot:
axins = zoomed_inset_axes(ax, 2, loc=1) # zoom = 2
axins.plot(ts)
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
plt.xticks(visible=False)
plt.yticks(visible=False)
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
plt.draw()
plt.savefig('daily_exchange_rates.pdf') # The zoomed window is empty!!!! 

推荐答案

我没用过pandas,但是我觉得问题出在你为axins.set_xlim(x1, x2),它们似乎超出了范围.我只是使用了 matplotlib 的绘图功能并更改了范围,并获得了缩放后的图像.

I have never used pandas, but I think that the problem is the range you are choosing for axins.set_xlim(x1, x2), they seem to be outside of the range. I just used the plotting capabilities of matplotlib and changed the range, and I obtained an image with the zoom.

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes 
from mpl_toolkits.axes_grid1.inset_locator import mark_inset

# Load the time series
ts = pd.read_csv('EUR_CHF_daily.csv',sep=';', parse_dates=['time'], index_col = 'time',decimal=',')
ts = ts['EUR/CHF']
ts = ts.sort_index(ascending=True)

# Plot
fig = plt.figure(figsize=(14,5))
ax = plt.axes()
ax.plot(ts)

# Label the axis
ax.set_xlabel('')
ax.set_ylabel('EUR/CHF')

#I want to select the x-range for the zoomed region. I have figured it out suitable values
# by trial and error. How can I pass more elegantly the dates as something like
x1 = 1543.90
x2 = 1658.80

# select y-range for zoomed region
y1 = 1.15
y2 = 1.25

# Make the zoom-in plot:
axins = zoomed_inset_axes(ax, 2, loc=1) # zoom = 2
axins.plot(ts)
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
plt.xticks(visible=False)
plt.yticks(visible=False)
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
plt.draw()
plt.show()

这篇关于Matplotlib/Pandas:使用时间序列缩放绘图的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)