In this article we want to learn that How to Use Matplotlib in Python, so Data visualization is an important part of data analysis. Matplotlib is a popular data visualization library in Python, it offers different tools and capabilities to create easy and informative visualizations. In this tutorial we want to talk about the basics of using Matplotlib.

**What is Matplotlib ?**

Matplotlib is a big data visualization library in Python, and it provides different plotting functions and tools. also Matplotlib allows you to create line plots, scatter plots, bar plots, histograms, pie charts and many other types of visualizations.

**Installing Matplotlib**

For working with Matplotlib, first of all we need to install this library and we can use pip for that.

1 |
pip install matplotlib |

**Matplotlib Line Plot Example**

Suppose we have a dataset and it contains the average monthly temperatures in a city over a year. We want to visualize the trend of temperature changes using a line plot. This is how we can do this using Matplotlib:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import matplotlib.pyplot as plt # Sample data months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] temperatures = [15, 17, 20, 23, 25, 30, 33, 32, 29, 25, 20, 18] # Create a line plot plt.plot(months, temperatures) # Customize the plot plt.title('Average Monthly Temperatures') plt.xlabel('Months') plt.ylabel('Temperature (°C)') # Display the plot plt.show() |

In the above example, we have first defined the dataset, with the months as labels and the corresponding average temperatures. after that we use the plot() function to create a line plot with the months on the x-axis and the temperatures on the y-axis. After customizing the plot with a title, x-axis label, and y-axis label, we use show() to display the plot.

Run the complete code and this will be the result

Other Types of Plots

Matplotlib offers different plot types and options. These are a few examples:

**Matplotlib Scatter Plot**

1 2 3 4 5 6 7 8 9 |
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.scatter(x, y) plt.title('Scatter Plot') plt.xlabel('X') plt.ylabel('Y') plt.show() |

**Matplotlib Bar Plot**

1 2 3 4 5 6 7 8 9 |
import matplotlib.pyplot as plt categories = ['A', 'B', 'C', 'D'] values = [10, 30, 20, 25] plt.bar(categories, values) plt.title('Bar Plot') plt.xlabel('Categories') plt.ylabel('Values') plt.show() |

**Matplotlib Histogram**

1 2 3 4 5 6 7 8 |
import matplotlib.pyplot as plt data = [1, 2, 3, 3, 4, 5, 5, 5, 6, 6, 7, 8, 8, 9] plt.hist(data, bins=5) plt.title('Histogram') plt.xlabel('Values') plt.ylabel('Frequency') plt.show() |

These examples are simple examples of what Matplotlib can do. You can check the library documentation and examples to learn more about its capabilities.