HomeLinuxMatplotlib Plot A number of Strains

Matplotlib Plot A number of Strains


“Matplotlib” is a flexible library that gives quite a lot of instruments to create/make varied sorts of plots. One of the frequent duties is plotting “a number of” traces in a “single” chart. On this article, we’ll discover alternative ways to plot a number of traces utilizing “Matplotlib” and customise the fashion, coloration, and labels to create efficient and visually interesting plots.

Making a Fundamental Line Plot

Earlier than diving into a number of line plots, let’s first create a easy line plot utilizing “Matplotlib”. To be able to get began, let’s import the important libraries and generate some pattern knowledge.

Instance
Within the following code, the “matplotlib.plot()” operate is utilized to make a fundamental line plot:

import matplotlib.pyplot as plt
import numpy
value1 = numpy.arange(0, 10, 0.1)
value2 = numpy.sin(value1)
plt.plot(value1, value2)
plt.present()

Within the above code snippet, the “np.arange()” operate is used to generate an array of values from “0” to “10” with a step of “0.1”. Equally, the “np.sin()” operate is used to calculate the sine of every worth within the handed array. To create a fundamental line plot, go the array and calculated sine of the array values because the “plt.plot()” operate arguments, respectively.

Output

The above output implies that the fundamental line plot has been created.

Plotting Single Horizontal Strains

To plot a single horizontal line, the “plt.axhline()” operate is utilized in Python.

Instance
For instance, let’s have a look at the next code:

import matplotlib.pyplot as plt
plt.axhline(y=2)
plt.present()

Within the above traces of code, the “axhline()” operate is used to plot a horizontal line at “y=2” on the present plot.

Output

Within the above output, the only horizontal line has been plotted efficiently.

Plotting Single Vertical Strains

To plot a single vertical line, the “plt.axvline()” operate is used as an alternative in Python.

Instance
Let’s overview the next instance code:

import matplotlib.pyplot as plt
plt.axvline(x=2)
plt.present()

Based on the above code block, the “plt.axvline()” operate is used to plot a vertical line at “x=2” on the present plot.

Output

As analyzed, the only vertical line has been plotted appropriately.

Plotting A number of Strains

To plot a number of traces, we merely go a number of “x” and “y” arrays to the “plt.plot()” operate.

Instance
The code is summarized beneath:

import matplotlib.pyplot as plt
import numpy
x = numpy.arange(0, 10, 0.1)
y1 = numpy.sin(x)
y2 = numpy.sin(2 * x)
plt.plot(x, y1)
plt.plot(x, y2)
plt.present()

Based on the above code, two arrays “y1” and “y2” are created with every representing a sine wave with a distinct frequency. The “plt.plot()” operate takes the array with every of the sine waves individually as an argument and creates two traces in the identical plot.

Output

Primarily based on the above output, the 2 traces in the identical plot have been generated efficiently.

Plotting A number of Strains by Customizing “linestyle” and “coloration” Arguments

In “Matplotlib”, by default, the plot is drawn with a stable blue line. The “linestyle” and “coloration” arguments can, nonetheless, be custom-made.

Instance
The beneath code is used to plot a number of traces with custom-made line kinds and colours:

import matplotlib.pyplot as plt
import numpy
x = numpy.arange(0, 10, 0.1)
y1 = numpy.sin(x)
y2 = numpy.sin(2 * x)
plt.plot(x, y1, linestyle=‘–‘, coloration=‘purple’)
plt.plot(x, y2, linestyle=‘:’, coloration=‘inexperienced’)
plt.present()

Within the above code, the “linestyle” is ready to ‘‘ for the primary line and ‘:‘ for the second, giving us dashed and dotted traces, respectively. Additionally, the colours are additionally set to purple and inexperienced for the primary and second traces, accordingly.

Output

Primarily based on the above output, a number of traces have been plotted through completely different line kinds and colours.

Conclusion

Matplotlib” supplies capabilities resembling “plt.plot()” to plot a number of traces on the identical plot. These multiple-line plots will also be plotted utilizing completely different line kinds and colours with the assistance of the custom-made “linestyle” and “coloration” arguments. This put up mentioned varied methods to plot a number of traces utilizing quite a few examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments