Generating a graph in Python can be a tricky affair, but a few simple steps can help you generate a graph easily. To generate graphs in Python you will need a library called Matplotlib. It helps in visualizing your data and makes it easier for you to see the relationship between different variables. Before starting with the graph, it is important to first understand Matplotlib and its functions in Python.
Why is data Visualization needed?
Visualization of data is a practice of presenting the data in a simple manner (in graphical or pictorial format), through which even a non-technical person can understand it easily as the human brain can process information easily when it is in pictorial or graphical form.
It allows us to quickly interpret data and adjust different variables to observe their effects. You can simply interpret the information from data visualization which is very helpful for a person (a Non-technical person) to understand.
Introduction to Matplotlib
Matplotlib is a library used in Python to generate graphs and lines for 2D graphics. Matplotlib package is totally written in Python. Matplotlib uses simple commands to generate simple plots for your data.
The first step is to install the Matplotlib using the pip command given below
pip install matplotlib
Using pip command it will take care of dependencies while installing the library in Python.
Matplotlib Python Plot
You might be thinking, to start with the plotting in Python there would be some typical commands which you will be using to generate graphs. Matplotlib has tremendously reduced that effort which provides a flexible library and much built-in defaults to simply generate graphs. You need to make sure that you make the necessary imports, prepare data and start with the plot() function.
Use this import to get started with your Matplotlib plot:>>> import matplotlib.pyplot as plt
To generate data later also NumPy import will be used. To import NumPy use this syntax:
>>> import numpy as npAlso, use show() function to show the resulting graph. Let us see a simple example of how we can start generating a graph.
You can also look for another example to generate a most simple graph using Matplotlib.
In the above example, we’ve just plotted a simple graph without any title, x-axis or y-axis. Moving forward we will be learning how to add title and labels to the graph.
Adding label and titles to your graph
In the above example, we’ve shown the x-axis and y-axis by a simple command plt.ylabel() and title by plt.title(). We have used plot(x,y) instead of using direct numbers for plotting the X and Y axis.
This graph doesn’t include any style or color. What if you want to add some style or change the width of the line or add color to the graph? We’ll see a simple code to generate a graph with different styles and colors.
Adding style to the graph
To introduce color in different lines we have used ‘g’ for green and ‘c’ for cyan. We can also introduce the thickness of the line by using linewidth function. As we have only used default grid lines, to change the color of the grid line use this simple command before plt.show()Result:
If you want to add a highlight to the graph which shows the details of the line you can use legend() function by using this simple command.Result:
After adding legend() and grid() function code will look like this.
In the above examples we have learned how to change width line, style, and grid or add a highlighter and now we’ll see how we can plot different types of graphs using Matplotlib in Python
Types of plots
There are several types of the plot which we will generate in this section using Matplotlib.
Bar graphs are used generally to compare different groups using visualizations. Whether it be a change of market or change in revenue, using a bar graph we can easily determine and compare the actual results.Code to generate Bar graph in Python:
Histogram: Histogram graph is generally used to display the statistical information or the distribution of successive process data set. The histogram is generally used for continuous data. Histogram or Bar graph may seem similar but a general difference between histogram plot and bar graph plot is that a histogram plot is used to display the distribution of variables while bar graph is used to display the comparison between variables.Code to generate the Histogram graph in Python:
As we have discussed various types of plots in the above section, we are going to see how we can work with multiple graphs.Code:
Now, you have learned how to generate graphs and what are the various types of plots which you can generate using Matplotlib in Python.