AttributeError: 'ErrorbarContainer' object has no attribute 'set_ylim'
<p>I am plotting the results of some experiments with error bars. I'd like to be able to set the y limit in the case of results with extreme outliers that aren't interesting. This code:</p>

<pre><code>axes = plt.errorbar(feature_data[feature_data.num_unique[feature_of_interest] &gt; 1].index, chi_square_y, yerr=chi_square_y_error, fmt = 'o')
axes.set_ylim([-.2, .2])
plt.plot((min(feature_data[feature_data.num_unique[feature_of_interest] &gt; 1].index), max(feature_data[feature_data.num_unique[feature_of_interest] &gt; 1].index)), (0, 0), 'r--', linewidth = 2)
</code></pre>

<p>produces this error:</p>

<pre><code>AttributeError                            Traceback (most recent call last)
&lt;ipython-input-79-794286dd3c29&gt; in &lt;module&gt;()
     18 rcParams['figure.figsize'] = 10, 5
     19 axes = plt.errorbar(feature_data[feature_data.num_unique[feature_of_interest] &gt; 1].index, chi_square_y, yerr=chi_square_y_error, fmt = 'o')
---&gt; 20 axes.set_ylim([-.2, .2])
     21 plt.plot((min(feature_data[feature_data.num_unique[feature_of_interest] &gt; 1].index), max(feature_data[feature_data.num_unique[feature_of_interest] &gt; 1].index)), (0, 0), 'r--', linewidth = 2)

AttributeError: 'ErrorbarContainer' object has no attribute 'set_ylim'
</code></pre>

<p>How can I set the y limits?</p>

<p>Thanks!</p>

<p>Since this is a bounty question I'll try to get a bit into more detail here. </p>

<p><code>plt.errorbar</code> does not return an Axes object (which has the <code>set_ylim</code> method), but rather a collection of <code>(plotline, caplines, barlinecols)</code>. I suspect you may have expected the Axes object since this is what <code>pandas.DataFrame.plot</code> returns.</p>

<p>When working directly with matplotlib's pyplot you have two options:</p>

<p><strong>Option 1</strong> - use pyplot directly, without dealing with the axes:</p>

<pre><code>plt.errorbar( ... )
plt.ylim([-.2, .2])
</code></pre>

<p>Using <code>plt</code> will set properties to the last subplot selected (by default there is only one). You may prefer this method when dealing with a single figure.</p>

<p><strong>Option 2</strong> - get an axes object from <code>subplots</code>:</p>

<pre><code>fig, ax = plt.subplots(1, 1, figsize=(10, 5))
ax.errorbar( ... )
ax.set_ylim([-.2, .2])
</code></pre>

<p>This is may preferred method, partly because it allows setting the figure size without setting it globally in <code>rcParams</code>. It has a few other advantages which I won't get into here.</p>

<p>Notice that when using <code>plt</code> the method is <code>ylim</code> and when using the Axes object it's <code>set_ylim</code>. This is true for many other properties such as titles, labels, etc.</p>

python matplotlib