Matplotlib: how to change colors, make small spaces and edit legend
I have the following code for drawing some lines in matplotlib. I tried to show that the points will be shown with transparent circles and not the standard solid circles.
- How do I get the circles to be the same color as the lines?
- How do I remove circles from the ends of dashed lines in a legend? Currently, you can hardly see the dotted lines.
-
How can I make a small space before and after each circle in the graph so that the dotted lines don't touch them. I think it will look better here as I only have data in points, so the lines between them do not 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 gives me.
This is what I would like it to ultimately look like (obviously with different data).
source to share
Remember that you seem to be using the fmt format string (for example "-go"
) incorrectly in your calls plot
. Actually for the dotted line, fmt should be larger than something like "--go"
. I personally find the use of keyword arguments is clearer, even if more verbose (in your case linestyle="dashed"
prevails in the fmt line)
http://matplotlib.org/api/axes_api.html?highlight=plot#matplotlib.axes.Axes.plot
Anyway, below is indicative for reproducing 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()
Also consider reading the legend guide in the official documentation: http://matplotlib.org/users/legend_guide.html?highlight=legend%20guide
source to share
For the first question, you use the attribute markeredgecolor
:
plt.plot(t, '-go', markerfacecolor='w', markeredgecolor='g', linestyle= 'dotted', label='n=20')
plt.plot(c, '-bo', markerfacecolor='w', markeredgecolor='b', linestyle= 'dotted', label='n=22')
As for the third question, I have no idea. I don't think there is an easy way to do this. But I think you could, for example, do the following:
- Highlight the line
- Flat white markers that are slightly larger than you have (edge ββand complexion white). They will mask parts of the line around the real markers.
- Sketch the real markers using the color you want.
source to share