Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'o-' mean in matplotlib's plot function?

I am using the following code which is drawing a simple line. I don't understand what's the meaning of this 'o-' inside the plot function.

import pylab as plt
import seaborn

x = np.linspace(0, 2, 10)
plt.plot(x, 'o-');
plt.show()
like image 785
Pumpkin C Avatar asked Aug 26 '26 00:08

Pumpkin C


2 Answers

These two characters are specifiers for the type of marker and the type of line you wish to have plotted.

The o will produce a small circle.

The - will produce a solid line to connect the markers.

For a full list of the types of specifiers, see this site: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot

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

You can also provide color indicators:

‘b’     blue
‘g’     green
‘r’     red
‘c’     cyan
‘m’     magenta
‘y’     yellow
‘k’     black
‘w’     white
like image 104
E. Ducateme Avatar answered Aug 27 '26 13:08

E. Ducateme


Until recently the documentation has not been very clear about the format strings. The good news is that it has been reworked completely now and includes a complete section explaining them.

I will simply cite from the new plot documentation:

Call signatures:

plot([x], y, [fmt], data=None, **kwargs)

Format Strings

A format string consists of a part for color, marker and line:

fmt = '[color][marker][line]'

Each of them is optional. If not provided, the value from the style cycle is used. Exception: If line is given, but no marker, the data will be a line without markers.

For a complete set of possible abbreviated format strings, see the documentation.

Example format strings:

'b'    # blue markers with default shape
'ro'   # red circles
'g-'   # green solid line
'--'   # dashed line with default color
'k^:'  # black triangle_up markers connected by a dotted line


Here, "o-" has the format fmt = '[marker][line]' and produces a dot as marker and a solid line to connect points.

Note that the format string might be confusing to use, so instead all options can be set via usual keyword arguments,

plot(y, fmt='[color][marker][line]')

is equivalent to

plot(y, color="[color]", marker="[marker]", linestyle="[line]")

So in this case, you might want to use

plot(y, marker="o", linestyle="-")
like image 21
ImportanceOfBeingErnest Avatar answered Aug 27 '26 15:08

ImportanceOfBeingErnest



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!