Unit-V 15
Unit-V 15
Matplotlib is a widely used library in Python for creating static, animated, and
interactive visualizations. It's especially popular for creating 2D plots like line graphs,
bar charts, scatter plots, histograms, and more.
To use Matplotlib, you first need to import it into your Python script or notebook. The
most common way to import Matplotlib is by using the alias plt for matplotlib.pyplot.
This simplifies the code, making it more readable and easier to type.
Explanation:
o This creates a line plot where x is the list of values for the x-axis, and y is
the corresponding list of values for the y-axis.
o plt.title(), plt.xlabel(), and plt.ylabel() add a title and labels to the x and y
axes, respectively.
o This displays the plot on the screen. In Jupyter Notebooks or IPython, the
plot may automatically show, but in standard Python scripts, plt.show() is
necessary to render the plot.
Sample Output
The above code will display a line plot with x-axis values [1, 2, 3, 4, 5] and corresponding
y-axis values [2, 3, 5, 7, 11], with a title and labelled axis.