Home > Software engineering >  How to zoom in a graph in Python using matplotlib or plotly?
How to zoom in a graph in Python using matplotlib or plotly?

Time:07-27

I want to zoom in to the red section of the graph. I have the following code snippet. What changes do I need to make in my code either using matplotlib or plotly to achieve the desired results. Please prefer plotly library if you can.

# Visualize decision tree predictions

predictions = treePrediction
valid = df[x.shape[0]:]
valid["Predictions"] = predictions
plt.figure(figsize=(12, 7))
plt.title("Apple's Stock Price Prediction Model(Decision Tree Regressor Model)")
plt.xlabel("Days")
plt.ylabel("Close Price USD ($)")
plt.plot(df["Mean"])
plt.plot(valid[["Mean", "Predictions"]])
plt.legend(["Original", "Valid", "Predictions"])
plt.show()

enter image description here

CodePudding user response:

Referring to the published node book, I have reproduced the graph in question. Since the nodebook was using locale CSV data, I retrieved the stock price data from yfinance and replaced the closing price with df['Mean']. Plotly, a graph_object was used to add each graph. I have also added a button to select the period for zooming. See this page for enter image description here

Select the most recent month using the Select Period button.

enter image description here

The range slider at the bottom of the graph can be used to select any range.

  • Related