Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib: how to change colors, make little spaces and edit the legend

I have the following code to draw some lines in matplotlib. I have tried to make the points be shown with transparent circles rather than the standard solid filled in circles.

  1. How can I make the circles be the same color as the lines?
  2. How can I remove the circles from the ends of the dashed lines in the legend? Currently you can hardly see the dashed lines.
  3. How can I make a little gap before and after each circle in the graph so the dashed lines don't touch them. I think this would look better here as I only have data at the points so the lines in between don't represent real data.

    import matplotlib.pyplot as plt
    import numpy as np
    t = np.array([0.19641715476064042,
    0.25,
    0.34,
    0.42])
    
    c  = np.array([0.17,
    0.21,
    0.27,
    0.36])
    
    plt.plot(t, '-go', markerfacecolor='w', linestyle= 'dashed', label='n=20')
    plt.plot(c, '-bo',  markerfacecolor='w', linestyle= 'dashed', label='n=22') 
    plt.show()
    

This is what the matplotlib code currently gives me.

enter image description here

This is what I would like it to ultimately look like (clearly with different data).

enter image description here

like image 833
graffe Avatar asked Jan 17 '26 15:01

graffe


1 Answers

Beware that you seem to misuse the fmt format string (like "-go") in your calls to plot. In fact for a dashed line fmt should be more something like "--go". I personally tend to find the use of keyword arguments clearer even if more verbose (in your case, linestyle="dashed" prevails on fmt string)

http://matplotlib.org/api/axes_api.html?highlight=plot#matplotlib.axes.Axes.plot

Anyway, below a tentative to reproduce the desired plot:

import matplotlib.pyplot as plt
import numpy as np

t = np.array([0.19641715476064042,
              0.25, 0.34, 0.42])
c = np.array([0.17, 0.21, 0.27, 0.36])

def my_plot(ax, tab, c="g", ls="-", marker="o", ms=6, mfc="w", mec="g", label="",
            zorder=2):
    """
    tab: array to plot
    c: line color (default green)
    ls: linestyle (default solid line)
    marker: kind of marker
    ms: markersize
    mfc: marker face color
    mec: marker edge color
    label: legend label
    """
    ax.plot(tab, c=c, ms=0, ls=ls, label=label, zorder=zorder-0.02)
    ax.plot(tab, c=c, marker=marker, ms=ms, mec=mec, mfc=mfc, ls="none",
            zorder=zorder)
    ax.plot(tab, c=c, marker=marker, ms=ms*4, mfc="w", mec="w", ls="none",
            zorder=zorder-0.01)


my_plot(plt, t, c="g", mec="g", ls="dashed", label="n=20")
my_plot(plt, c, c="b", mec="b", ls="dashed", label="n=22")

plt.legend(loc=2) 
plt.show()

enter image description here

Also consider reading the legend guide in the official documentation: http://matplotlib.org/users/legend_guide.html?highlight=legend%20guide

like image 70
GBy Avatar answered Jan 20 '26 06:01

GBy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!