How it works…

The line plots with different line styles are produced with the following code:

plt.plot(xvalues, ls = '-', color='green')
plt.plot(xvalues + 1, ls = '--', color='green')
plt.plot(xvalues + 2, ls = ':', color='green')
plt.plot(xvalues + 3, ls = '-.', color='green')

The line style is set with the ls option, which accepts the values shown in the following table:

Option

Line style

'-' or 'solid'

Solid line

'--' or 'dashed'

Dashed line

'-.' or 'dashdot'

Dashed-dotted line

':' or 'dotted'

Dotted line
None, ' ' or '' Blank line

 

In the second subplot, we demonstrate the use of markers with the following code:

plt.plot(xvalues, marker='o', ls='', color='blue')
plt.plot(xvalues + 1, marker='^', ls='', color='blue')
plt.plot(xvalues + 2, marker='*', ls='--', color='blue')
plt.plot(xvalues + 3, marker='>', ls=':', color='blue',
mec='black', mfc='red', ms=15)

Markers are specified with the marker option and a wide variety of marker shapes are supported. The possible values for the marker option are listed at https://matplotlib.org/api/markers_api.html#module-matplotlib.markers.

Markers and line styles are independent and must be set with different options. In the first two preceding examples, we use ls='' to specify a blank line, that is, no connecting lines are drawn between the points. In the next two lines of code, we use, respectively, a dashed line and a dotted line.

In the last example, we used the mec='black', mfc='red', and ms=15 options to, respectively, set the marker edge color to black, the marker face color to red, and the marker size to 15.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset