This series will introduce you to graphing in python with Matplotlib, which is arguably the most popular graphing and data visualization library for Python.
Easiest way to install matplotlib is to use pip. Type following command in terminal:
pip install matplotlib
OR, you can download it from here and install it manually.
Getting started ( Plotting a line)
The code seems self explanatory. Following steps were followed:
- Define the x-axis and corresponding y-axis values as lists.
- Plot them on canvas using .plot() function.
- Give a name to x-axis and y-axis using .xlabel() and .ylabel() functions.
- Give a title to your plot using .title() function.
- Finally, to view your plot, we use .show() function.
Plotting two or more lines on same plot
- Here, we plot two lines on same graph. We differentiate between them by giving them a name(label) which is passed as an argument of .plot() function.
- The small rectangular box giving information about type of line and its color is called legend. We can add a legend to our plot using .legend() function.
Customization of Plots
Here, we discuss some elementary customizations applicable on almost any plot.
As you can see, we have done several customizations like
- setting the line-width, line-style, line-color.
- setting the marker, marker’s face color, marker’s size.
- overriding the x and y axis range. If overriding is not done, pyplot module uses auto-scale feature to set the axis range and scale.
- Here, we use plt.bar() function to plot a bar chart.
- x-coordinates of left side of bars are passed along with heights of bars.
- you can also give some name to x-axis coordinates by defining tick_labels
- Here, we use plt.hist() function to plot a histogram.
- frequencies are passed as the ages list.
- Range could be set by defining a tuple containing min and max value.
- Next step is to “bin” the range of values—that is, divide the entire range of values into a series of intervals—and then count how many values fall into each interval. Here we have defined bins = 10. So, there are a total of 100/10 = 10 intervals.
- Here, we use plt.scatter() function to plot a scatter plot.
- Like a line, we define x and corresponding y – axis values here as well.
- marker argument is used to set the character to use as marker. Its size can be defined using s parameter.
Output of above program looks like this:
- Here, we plot a pie chart by using plt.pie() method.
- First of all, we define the labels using a list called activities.
- Then, portion of each label can be defined using another list called slices.
- Color for each label is defined using a list called colors.
- shadow = True will show a shadow beneath each label in pie-chart.
- startangle rotates the start of the pie chart by given degrees counterclockwise from the x-axis.
- explode is used to set the fraction of radius with which we offset each wedge.
- autopct is used to format the value of each label. Here, we have set it to show the percentage value only upto 1 decimal place.
Plotting curves of given equation
Output of above program looks like this:
Here, we use NumPy which is a general-purpose array-processing package in python.
- To set the x – axis values, we use np.arange() method in which first two arguments are for range and third one for step-wise increment. The result is a numpy array.
- To get corresponding y-axis values, we simply use predefined np.sin() method on the numpy array.
- Finally, we plot the points by passing x and y arrays to the plt.plot() function.
So, in this part, we discussed various types of plots we can create in matplotlib. There are more plots which haven’t been covered but the most significant ones are discussed here –
This article is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Graph Plotting in Python | Set 3
- Graph Plotting in Python | Set 2
- Plotting graph using Seaborn | Python
- Python | Geographical plotting using plotly
- SunPy | Plotting a Solar Image in Python
- Python | Plotting Google Map using folium package
- Python | Plotting Google Map using gmplot package
- Python | Matplotlib Sub plotting using object oriented API
- Python | Plotting Fibonacci spiral fractal using Turtle
- Python | Plotting Data on Google Map using pygmaps package
- Python | Plotting charts in excel sheet using openpyxl module | Set - 1
- Python | Plotting Pie charts in excel sheet using XlsxWriter module
- Python | Plotting bar charts in excel sheet using XlsxWriter module
- Python | Plotting charts in excel sheet using openpyxl module | Set – 2
- Python | Plotting charts in excel sheet using openpyxl module | Set 3