0

I have the following code...

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(seed=2021)

x = np.arange(0, 200)
y = np.random.randint(1, 10, 200)

plt.plot(x, y)

plt.show()

...that generates this chart:

enter image description here

What I need is when the code start, the chart zooms by default to the last 25 records.

enter image description here

I do not want to limit the data. I want the 200 records to continue graphing, so that later I can move through the chart (with the arrow in the lower left corner) in case I want to see the historical data.

Is there a way to set a default zoom when the chart start?

Ricardo Carrera
  • 113
  • 2
  • 10
  • 1
    how about `plt.xlim(175, 200)` – furas Dec 10 '21 at 00:46
  • Does this answer your question? [Python, Matplotlib, subplot: How to set the axis range?](https://stackoverflow.com/questions/2849286/python-matplotlib-subplot-how-to-set-the-axis-range) – BigBen Dec 10 '21 at 00:54

2 Answers2

1

You can just add plt.xlim(175, 200) which sets the limit of the x axis.

Jake
  • 915
  • 1
  • 7
  • 22
1

If you do not want to hard code the left limit in xlim, you can read the shape of your x array and use this to plot only the last 25 samples:

plt.xlim(left=x.shape[0]-25)

Sheldon
  • 4,084
  • 3
  • 20
  • 41