Contents

English translation

Contents

Matplotlib

import numpy as np
import matplotlib.pyplot as plt

1 Plotting

# For the points to plot,
# store the x-coordinate and y-coordinate values in variables
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
# Plot the points using the plot function
plt.plot(x, y)
# Display the plot on screen
plt.show()
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Initialize the plot
plt.clf()
plt.plot(x, y_sin)
plt.plot(x, y_cos)
# Display axis names
plt.xlabel('x axis')
plt.ylabel('y axis')
# Display the title
plt.title('Sine and Cosine')
# Display the legend - using a list
# The list length must equal the number of plotted series
plt.legend(['Sine', 'Cosine'])
plt.show()

2 Multiple plots

plt.clf()
# Adjust spacing between plots
left=0.0
bottom=0.0
right=1.0
top=1.0
wspace=1.0
hspace=1.0
plt.subplots_adjust(left, bottom, right, top, wspace, hspace)
# Declare a layout with 2 rows and 1 column of plots
# Create a plot in position 1
plt.subplot(2,1,1)
plt.plot(x,y_sin)
plt.title('Sine')
plt.xlabel('x axis')
plt.ylabel('y axis')
# Create a plot in position 2
plt.subplot(2,1,2)
plt.plot(x,y_cos)
plt.title('Cosine')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.show()

3 Working with text

plt.clf()
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])
# text(a,b,s) : display text s at position a, b
plt.text(4,0.5, 'here!')
# Express an equation as text
plt.text(6,0.5,r'$y=sin(x)$')
plt.show()
plt.clf()
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])
plt.annotate('min', xy=(np.pi, -1), xytext=(5, -0.5), 
             arrowprops=dict(facecolor='black', shrink=0.05),)
plt.show()

4 Practice