Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set markers for individual points on a line in Matplotlib

I have used Matplotlib to plot lines on a figure. Now I would now like to set the style, specifically the marker, for individual points on the line. How do I do this?

To clarify my question, I want to be able to set the style for individual markers on a line, not every marker on said line.

like image 624
dbmikus Avatar asked Dec 07 '11 00:12

dbmikus


People also ask

How do I label a specific point in matplotlib?

To label the scatter plot points in Matplotlib, we can use the matplotlib. pyplot. annotate() function, which adds a string at the specified position.


1 Answers

Specify the keyword args linestyle and/or marker in your call to plot.

For example, using a dashed line and blue circle markers:

plt.plot(range(10), linestyle='--', marker='o', color='b', label='line with marker') plt.legend() 

A shortcut call for the same thing:

plt.plot(range(10), '--bo', label='line with marker') plt.legend() 

enter image description here

Here is a list of the possible line and marker styles:

================    =============================== character           description ================    ===============================    -                solid line style    --               dashed line style    -.               dash-dot line style    :                dotted line style    .                point marker    ,                pixel marker    o                circle marker    v                triangle_down marker    ^                triangle_up marker    <                triangle_left marker    >                triangle_right marker    1                tri_down marker    2                tri_up marker    3                tri_left marker    4                tri_right marker    s                square marker    p                pentagon marker    *                star marker    h                hexagon1 marker    H                hexagon2 marker    +                plus marker    x                x marker    D                diamond marker    d                thin_diamond marker    |                vline marker    _                hline marker ================    =============================== 

edit: with an example of marking an arbitrary subset of points, as requested in the comments:

import numpy as np import matplotlib.pyplot as plt  xs = np.linspace(-np.pi, np.pi, 30) ys = np.sin(xs) markers_on = [12, 17, 18, 19] plt.plot(xs, ys, '-gD', markevery=markers_on, label='line with select markers') plt.legend() plt.show() 

enter image description here

This last example using the markevery kwarg is possible in since 1.4+, due to the merge of this feature branch. If you are stuck on an older version of matplotlib, you can still achieve the result by overlaying a scatterplot on the line plot. See the edit history for more details.

like image 146
wim Avatar answered Sep 18 '22 00:09

wim