36 Linecharts
36 Linecharts
First import Matplotlib.pyplot library for plotting functions. Also, import the
Numpy library as per requirement. Then define data values x and y.
we can see in the above output image that there is no label on the x-axis
and y-axis. Since labeling is necessary for understanding the chart
dimensions. In the following example, we will see how to add labels, Ident
in the charts
plt.plot(x, y)
plt.xlabel("X-axis") # add X-axis label
plt.ylabel("Y-axis") # add Y-axis label
plt.title("Any suitable title") # add title
plt.show()
Multiple charts:
We can display more than one chart in the same container by
using pyplot.figure() function. This will help us in comparing the different
charts and also control the look and feel of charts .
x = np.array([1, 2, 3, 4])
y = x*2
plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Any suitable title")
plt.show() # show first chart
x = np.array([1, 2, 3, 4])
y = x*2
x1 = [2, 4, 6, 8]
y1 = [3, 5, 7, 9]
x = np.array([1, 2, 3, 4])
y = x*2
plt.plot(x, y)
x1 = [2, 4, 6, 8]
y1 = [3, 5, 7, 9]
plt.fill_between(x, y, y1,
color='green', alpha=0.5)
plt.show()
Some more Examples:
Linestyle
You can use the keyword argument linestyle, or shorter ls, to change the style of
the plotted line:
Line Color
You can use the keyword argument color or the shorter c to set the color of the line:
Line Width
You can use the keyword argument linewidth or the shorter lw to change the width
of the line.
Multiple Lines:
import matplotlib.pyplot as plt
import numpy as np
x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 10])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([6, 2, 7, 11])