To know what you are looking at, you need meta data. Labels are a type of meta data. They show what the chart is about. The chart has an x label, y label and title.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import matplotlib.pyplot as plt import numpy as np
plt.xlabel("I am x") plt.ylabel("I am y") plt.title("With Labels")
plt.show()
Multiple lines
More than one line can be in the plot. To add another line, just call the plot(x,y) function again. In the example below we have two different values for y (y1,y2) that are plotted onto the chart.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import matplotlib.pyplot as plt import numpy as np
Lines can be in the form of dots like the image below. Instead of calling plot(x,y) call the scatter(x,y) method. The scatter(x,y) method can also be used to (randomly) plot points onto the chart.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import matplotlib.pyplot as plt import numpy as np
n = 1024 X = np.random.normal(0, 1, n) Y = np.random.normal(0, 1, n) T = np.arctan2(X, Y)
for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(12) label.set_bbox(dict(facecolor='y', edgecolor='None', alpha=0.7))
plt.show()
Line with asymptote
An asymptote can be added to the plot. To do that, use plt.annotate(). There’s lso a dotted line in the plot below. You can play around with the code to see how it works.
It doesn’t have to be a numeric scale. The scale can also contain textual words like the example below. In plt.yticks() we just pass a list with text values. These values are then show against the y axis.