matplotlib tutorial (2) nitta@tsuda.ac.jp

Chapter 2: Drawing a line graph

2-1: Draw a line graph by specifying the x-axis and y-axis values

2-2: Set x-axis and y-axis labels

2-3: Specify the width, color, and type of the line graph

2-4: Display multiple line graphs with line names

Specify label=text in the argument of ax.plot() in drawing each line. Then call the ax.legend() function to display the legend for the coordinate system.

2-5 Change the font.

If the formula is displayed with the default font, it will be difficult to read, so specify a font suitable for mathematics. Please note that the available fonts differ depending on the version of matplotlib.

We used the settings below for a while, but we got an error in Google Colab, so we changed it like sample code 2-5.

# for local jupyter notebook (not Google Colabe)
plt.rcParams['font.family'] = 'Times New Roman'
plt.rcParams['mathtext.fontset'] = 'cm'

2-6: Set the style of the markers

2-7: Erase the lines to make a scatter diagram.

To remove the lines from the line graph, specify linestyle='None' as an argument of the plot() function call.

2-8: Color the top and bottom of the lines differently (numpy.ma.masked_where)

By masking the y value with numpy.ma.masked_where(), the upper and lower sides of the line can be displayed in different colors.