# sample code 2-1
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
xs = [1, 2, 3, 4]
ys = [1, 4, 9, 16]
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, ys)
plt.show()
# sample code 2-2
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
xs = [1, 2, 3, 4]
ys = [1, 4, 9, 16]
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, ys)
ax.set_ylabel('y-label')
ax.set_xlabel('x-label')
plt.show()
# sample code 2-3
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
xs = [1, 2, 3, 4]
ys = [1, 4, 9, 16]
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, ys, linestyle='dotted', linewidth=5, color='red')
ax.set_ylabel('y-label')
ax.set_xlabel('x-label')
plt.show()
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.
# sample code 2-4
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
xs = np.array([1, 2, 3, 4])
ys = np.array([1, 4, 9, 16])
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, ys, linestyle='solid', label='$y=x^2$')
ax.plot(xs, ys/2, linestyle='dashed', label='$y=x^2 / 2$')
ax.plot(xs, ys/3, linestyle='dashdot', label='$y = x^2 / 3$')
ax.plot(xs, ys/4, linestyle='dotted', label='$y=x^2/4$')
ax.set_ylabel('y-label')
ax.set_xlabel('x-label')
ax.legend()
plt.show()
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'
# sample code 2-5
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = [ 'DejaVu Sans' ]
plt.rcParams['mathtext.fontset'] = 'cm'
xs = np.array([1, 2, 3, 4])
ys = np.array([1, 4, 9, 16])
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, ys, linestyle='solid', label='$y=x^2$')
ax.plot(xs, ys/2, linestyle='dashed', label='$y=x^2 / 2$')
ax.plot(xs, ys/3, linestyle='dashdot', label='$y = x^2 / 3$')
ax.plot(xs, ys/4, linestyle='dotted', label='$y=x^2/4$')
ax.set_ylabel('y-label')
ax.set_xlabel('x-label')
ax.legend()
plt.show()
# sample code 2-6
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = [ 'DejaVu Sans' ]
xs = np.array([1, 2, 3, 4])
ys = np.array([1, 4, 9, 16])
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, ys, label='$y=x^2$', marker='o')
ax.plot(xs, ys/2, label='$y=x^2 / 2$', marker='d')
ax.plot(xs, ys/3, label='$y = x^2 / 3$', marker='v')
ax.plot(xs, ys/4, label='$y=x^2/4$', marker='*')
ax.set_ylabel('y-label')
ax.set_xlabel('x-label')
ax.legend()
plt.show()
To remove the lines from the line graph, specify linestyle='None'
as an argument of the plot()
function call.
# sample code 2-7
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = [ 'DejaVu Sans' ]
xs = np.array([1, 2, 3, 4])
ys = np.array([1, 4, 9, 16])
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, ys, label='$y=x^2$', marker='o', linestyle='None')
ax.plot(xs, ys/2, label='$y=x^2 / 2$', marker='d', linestyle='None')
ax.plot(xs, ys/3, label='$y = x^2 / 3$', marker='v', linestyle='None')
ax.plot(xs, ys/4, label='$y=x^2/4$', marker='*', linestyle='None')
ax.set_ylabel('y-label')
ax.set_xlabel('x-label')
ax.legend()
plt.show()
By masking the y value with numpy.ma.masked_where()
, the upper and lower sides of the line can be displayed in different colors.
# sample code 2-8
# draw lines in different colors
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-np.pi, np.pi, 0.001)
y = np.sin(4 * x) + x/5
upper = 0.75
lower = -0.75
set_upper = np.ma.masked_where(y<upper, y) # Leave values greater than or equal to 'upper'
set_lower = np.ma.masked_where(y>lower, y) # Leave values less than or equal to 'lower'
# Leave values between 'lower' and 'upper'
set_middle = np.ma.masked_where(np.logical_or(y > upper, y<lower), y)
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.plot(x, set_upper, color='red')
ax.plot(x, set_lower, color='blue')
ax.plot(x, set_middle, color='black')
plt.show()