python - Zoomed Plot with Time Series -
python - Zoomed Plot with Time Series -
my task simple: have time series ts
(euro swiss franc daily exchange rates between 2010 , 2014) plot. in plot highlight time interval zooming it. however, zoomed window stays empty (see code below). furthermore, have problem selecting x-range of zoomed-in window since not know how transform dates internal integer representation of matplotlib.
thanks in advance help!
here code:
import numpy np import pandas pd matplotlib import pyplot plt mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes mpl_toolkits.axes_grid1.inset_locator import mark_inset # load 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 time series # label axis ax.set_xlabel('') ax.set_ylabel('eur/chf') #i want select x-range zoomed region. have figured out suitable values # trial , error. how can pass more elegantly dates # x1 = '2012-05-09' # x2 = '2012-09-02' x1 = 15439.0 x2 = 15588.0 # select y-range zoomed part y1 = 1.15 y2 = 1.25 # create 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') # zoomed window empty!!!!
i have never used pandas
, think problem range choosing axins.set_xlim(x1, x2)
, seem outside of range. used plotting capabilities of matplotlib
, changed range, , obtained image zoom.
import numpy np import pandas pd matplotlib import pyplot plt mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes mpl_toolkits.axes_grid1.inset_locator import mark_inset # load 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 axis ax.set_xlabel('') ax.set_ylabel('eur/chf') #i want select x-range zoomed region. have figured out suitable values # trial , error. how can pass more elegantly dates x1 = 1543.90 x2 = 1658.80 # select y-range zoomed part y1 = 1.15 y2 = 1.25 # create 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()
python matplotlib pandas time-series
Comments
Post a Comment