Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding error bars to Matplotlib-generated graph of Pandas dataframe creates invalid legend
    text
    copied!<p>I am trying to graph a Pandas dataframe using Matplotlib. The dataframe contains four data columns composed of natural numbers, and an index of integers. I would like to produce a single plot with line graphs for each of the four columns, as well as error bars for each point. In addition, I would like to produce a legend providing labels for each of the four graphed lines.</p> <p>Graphing the lines and legend without error bars works fine. When I introduce error bars, however, the legend becomes invalid -- the colours it uses no longer correspond to the appropriate lines. If you compare a graph with error bars and a graph without, the legend and the shapes/positions of the curves remain exactly the same. The <em>colours</em> of the curves get switched about, however, so that though the same four colours are used, they now correspond to different curves, meaning that the legend now assigns the wrong label to each curve.</p> <p>My graphing code is thus:</p> <pre><code>def plot_normalized(agged, show_errorbars, filename): combined = {} # "agged" is a dictionary containing Pandas dataframes. Each dataframe # contains both a CPS_norm_mean and CPS_norm_std column. By running the code # below, the single dataframe "combined" is created, which has integer # indices and a column for each of the four CPS_norm_mean columns contained # in agged's four dataframes. for k in agged: combined[k] = agged[k]['CPS_norm_mean'] combined = pandas.DataFrame(combined) plt.figure() combined.plot() if show_errorbars: for k in agged: plt.errorbar( x=agged[k].index, y=agged[k]['CPS_norm_mean'], yerr=agged[k]['CPS_norm_std'] ) plt.xlabel('Time') plt.ylabel('CPS/Absorbency') plt.title('CPS/Absorbency vs. Time') plt.savefig(filename) </code></pre> <p>The <a href="https://gist.github.com/4145371/f6b4f3868b00d07e4a33aa8665de3c3338fbfd4c" rel="nofollow">full 100-line script</a> is available on GitHub. To run, download both graph.py and lux.csv, then run "python2 graph.py". It will generate two PNG files in your working directory -- one graph with error bars and one without.</p> <p>The graphs are thus:</p> <ul> <li>Correct graph (with no error bars): i.imgur.com/XE3oY.png</li> <li>Incorrect graph (with error bars): i.imgur.com/OQVQL.png</li> </ul> <p>Observe that the graph without error bars is properly labelled; note that the graph <em>with</em> error bars is improperly labelled, as though the legend is identical, the line graphs' changed colours mean that each legend entry now refers to a different (wrong) curve. </p> <p>Thanks for any help you can provide. I've spent a number of extremely aggravating hours bashing my head against the wall, and I suspect that I'm making a stupid beginner's mistake. For what it's worth, I've tried with the Matplotlib development tree, version 1.2.0, and 1.1.0, and all three have exhibited identical behaviour.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload