Limit data range without using xlim - python, matplotlib
<p>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. <strong>In other words, I want to set limits on the data itself, not the axis.</strong> </p>

<pre><code>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')
</code></pre>

<p>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 <code>plt.plot_date</code> line, but I don't know what to add.</p>

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

<p>For instance something like</p>

<p><code>data_for_plotting=DF.query("date&gt;'1925-01-01' and date&lt;'1940-01-01'")</code></p>

<p>Then pass that instead of DF to the rest of the plotting statements.</p>

<p>You can look at <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html" rel="nofollow">http://pandas.pydata.org/pandas-docs/stable/indexing.html</a> for other ways to do the same thing.</p>

python matplotlib