mnbvcxzsdfghjkl mnbvcxzsdfghjkl - 1 month ago 15x
Python Question

Limit data range without using xlim - python, matplotlib

I want to put multiple lines on the same plot, but use only part of the data available for some of the lines. Each dataset contains data from 1925 until the present, and I'd like the x-axis to show that entire range, but I only want to show dataset A from 1925 until 1940, dataset B from 1941 to 1958, and so on. In other words, I want to set limits on the data itself, not the axis.

fig, ax = plt.subplots(figsize=(15,10))

plt.plot_date(DF['date'], DF['data1'], '.')
plt.plot_date(DF['date'], DF['data2'], '.')
plt.plot_date(DF['date'], DF['data3'], '.')
plt.plot_date(DF['date'], DF['data4'], '.')

ylabel('Mean Streambed Elevation (feet)')
plt.xlim('1925-01-01', '2020-01-01')

All of my searching has just found questions that are solved by using xlim(), but that's not what I'm looking for. I figure that the solution will be to add something to each
line, but I don't know what to add.


There are many ways to select subsets of a pandas dataframe, assuming that's what you have.

For instance something like

data_for_plotting=DF.query("date>'1925-01-01' and date<'1940-01-01'")

Then pass that instead of DF to the rest of the plotting statements.

You can look at for other ways to do the same thing.