Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: How to make a dotted line consisting of dots (circles)?

I have two smooth dependences y1(x) and y2(x) where x's are distributed irregularly. I want these dependences to be described with dotted lines (linestyle = ':'). What i get now in a *.pdf file is shown here:

Here's the code:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x  = [0, 1, 2,  3,  5,  7, 13, 14]
y1 = [3, 5, 6,  8,  7,  6,  9, 10]
y2 = [1, 7, 8, 10, 14, 18, 20, 23]

ax.plot(x, y1, 
        linestyle = ':',
        linewidth = 4,
        color = 'Green')

ax.plot(x, y2, 
        linestyle = ':',
        linewidth = 4,
        color = 'Blue')

ax.set_ylabel('y(x)')
ax.set_xlabel('x')

plt.savefig("./test_dotted_line.pdf")

I played with dashes = [2,2] (and other combinations) and dash_capstyle = 'round', but the result looks bad.

Is there a chance to have a dotted line consisting of 'circle' dots?

like image 450
glanz Avatar asked Mar 12 '23 11:03

glanz


2 Answers

try this linestyle:

ax.plot(x, y1, 
    linestyle = (0,(0.1,2)),
    dash_capstyle = 'round',
    linewidth = 4,
    color = 'Green')

output is like this: output

like image 89
ZHANG Wuji Avatar answered Mar 14 '23 01:03

ZHANG Wuji


This does the trick

ax.plot(x, y1, linestyle = '--',
    linewidth = 4,
    color = 'Green',
    dashes=(0.5, 5.),
    dash_capstyle='round'

)

You have use dashes, set dash_capstyle='round', and then play with the dashes=(ink_points_on, ink_points_off) to get dots of the size you want.

like image 27
andres Avatar answered Mar 14 '23 00:03

andres