Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:08:27+00:00 2026-06-14T23:08:27+00:00

I am trying to graph a Pandas dataframe using Matplotlib. The dataframe contains four

  • 0

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.

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 colours 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.

My graphing code is thus:

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)

The full 100-line script 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.

The graphs are thus:

  • Correct graph (with no error bars):
  • Incorrect graph (with error bars):

Observe that the graph without error bars is properly labelled; note that the graph with 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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:08:28+00:00Added an answer on June 14, 2026 at 11:08 pm

    I am new to programming and python in general but I managed to throw together a dirty fix, the legends are now correct, the colors are not.

    def plot_normalized(agged, show_errorbars, filename):
      combined = {}
      for k in agged:
        combined[k] = agged[k]['CPS_norm_mean']
      combined = pandas.DataFrame(combined)
    
      ax=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'],
            label = k #added
          )
    
      if show_errorbars: #try this, dirty fix
       labels, handles = ax.get_legend_handles_labels()
       N = len(handles)/2
       plt.legend(labels[:N], handles[N:])
    
      #Why does the fix work?:
      #labels, handles = ax.get_legend_handles_labels()
      #print handles
      #out:
      #[u'Blank', u'H9A', u'Q180K', u'Wildtype', 'Q180K', 'H9A', 'Wildtype', 'Blank']
      #Right half has correct order, these are the labels from label=k above in errorplot
    
    
    
      plt.xlabel('Time')
      plt.ylabel('CPS/Absorbency')
      plt.title('CPS/Absorbency vs. Time')
      plt.savefig(filename)
    

    Produces:
    No error plot
    Error plot

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to display a real time graph using the data which is
I have been trying to graph my data set using GNUPLOT, but so far
I'm currently trying to graph a sphere in a tkinter window using matplotlib. How
I'm trying to plot a graph in matplotlib using numpy and meshgrid. I want
I am trying to graph families of curves using Matplotlib. I am graphing the
I am trying to graph data that I am pulling from an ActiveRecord query
I'm trying to publish checkin using Facebook Graph API. I've gone through Facebook API
I am trying to plot a graph using gnuplot. I have six text files.
I'm trying to program a graph class using an adjacent list from an example
I have data in data table like below. I am trying to make graph

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.