HarriS
HarriS

Reputation: 850

matplotlib: Changing x limit dates

I would like to be able to change the x limits so it shows a time frame of my choice.

reproducible example:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# libraries for Data Download
import datetime # if you want to play with the dates
from pandas_datareader import data as pdr
import yfinance as yf

df = pdr.get_data_yahoo('ETH-USD', interval = '1d', period = "5y")
plt.figure(figsize=(24,10), dpi=140)
plt.grid()
df['Close'].plot()
df['Close'].ewm(span=50).mean().plot(c = '#4d00ff')
df['Close'].ewm(span=100).mean().plot(c = '#9001f0')
df['Close'].ewm(span=200).mean().plot(c = '#d102e8')
df['Close'].ewm(span=300).mean().plot(c = '#f101c2')
df['Close'].rolling(window=200).mean().plot(c = '#e80030')
plt.title('ETH-USD PLOT',fontsize=25, ha='center')
plt.legend(['C L O S E', 'EMA 50','EMA 100','EMA 200','EMA 300','MA 200', ])
# plt.xlim(['2016-5','2017-05']) # My attempt 
plt.show()

plot

when un-commenting the line above I get:

plot2

I would have liked '2016-5' to '2017-05' to have taken up the whole plot so I can see more detail.

Upvotes: 0

Views: 228

Answers (1)

dm2
dm2

Reputation: 4275

It seems to me that you xlim works well, however, if I understand your question correctly, you also need to adjust ylim (let's say (0,100) from your graph, as it doesn't seem data within the time period specified goes past value of 100) to stretch data vertically, and so fill the graph efficiently.

try adding plt.ylim((0,100)) together with your commented code

Output:

with your plt.xlim(['2016-5','2017-05']) and plt.ylim((0,100))

enter image description here

with your plt.xlim(['2016-5','2017-05']) and plt.ylim((0,40))

enter image description here

as you can see, due to data variance in the period, you might lose some data information at later dates or have less clear image of movement at earlier dates.

Upvotes: 1

Related Questions