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 8887861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:54:45+00:00 2026-06-14T21:54:45+00:00

I currently generate my legend with matplotlib this way: if t==25: l1,l2 = ax2.plot(x320,vTemp320,’or’,x320,vAnaTemp320,’-r’)

  • 0

I currently generate my legend with matplotlib this way:

if t==25:
    l1,l2 = ax2.plot(x320,vTemp320,'or',x320,vAnaTemp320,'-r')
elif t==50:
    l3,l4 = ax2.plot(x320,vTemp320,'ob',x320,vAnaTemp320,'-b')
else:
    l5,l6 = ax2.plot(x320,vTemp320,'og',x320,vAnaTemp320,'-g')
plt.legend((l1,l2,l3,l4,l5,l6), ('t=25 Simulation', 't=25 Analytical','t=50 Simulation', 't=50 Analytical','t=500 Simulation', 't=500 Analytical'),
   bbox_to_anchor=(-.25, 1), loc=2, borderaxespad=0.,prop={'size':12})

Which somehow works see below. But I have duplicated information in my legend.

I would prefer to separate the legend so that I have different colored lines corresponding to each time t, and a normal line as my Analytical solution and dots for the results of my simulation.

Something like that

--(red line)   t = 25
--(blue line)  t = 50
--(green line) t = 500
o              Simulaton
--             Analytical Solution

Does anyone know how I could achieve this with matplotlib?

The current image

  • 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-14T21:54:47+00:00Added an answer on June 14, 2026 at 9:54 pm

    You can chose the artists and labels to display in the legend as follows. You’ll need to create custom artists for the elements in the legend that are not actually plotted.

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0,10,31)
    
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    
    #Plot analytic solution
    ax.plot(x,1*x**2, color='r', label="t = 25")
    ax.plot(x,2*x**2, color='b', label="t = 50")
    ax.plot(x,3*x**2, color='g', label="t = 500")
    
    #Plot simulation
    ax.plot(x,1*x**2, color='r', linestyle='', marker='o')
    ax.plot(x,2*x**2, color='b', linestyle='', marker='o')
    ax.plot(x,3*x**2, color='g', linestyle='', marker='o')
    
    #Get artists and labels for legend and chose which ones to display
    handles, labels = ax.get_legend_handles_labels()
    display = (0,1,2)
    
    #Create custom artists
    simArtist = plt.Line2D((0,1),(0,0), color='k', marker='o', linestyle='')
    anyArtist = plt.Line2D((0,1),(0,0), color='k')
    
    #Create legend from custom artist/label lists
    ax.legend([handle for i,handle in enumerate(handles) if i in display]+[simArtist,anyArtist],
              [label for i,label in enumerate(labels) if i in display]+['Simulation', 'Analytic'])
    
    plt.show()
    

    Example plot

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

Sidebar

Related Questions

Currently I generate an URL to a different portlet in this fashion: <liferay-portlet:actionURL var=custom
Is there currently a way to generate XML files containing tables compatible with Microsoft
Currently I'm using the BouncyCastle library to generate a certificate. Something like this: X509V3CertificateGenerator
I currently use phpthumb to generate thumbnails for profile pictures. http://phpthumb.gxdlabs.com/ This is my
Currently I am doing something like this in my code: --Generate a list of
I am currently trying to generate the creation SQL for my tables based off
Currently I'm using Fluent NHibernate to generate my database schema, but I want the
currently i'm trying to use imagick to generate some images without saving them on
Currently, I use value converters to generate user-friendly strings for the GUI. As an
I'm currently working a problem that requires my web application to generate a chart

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.