top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

A Quick start guide to MATPLOTLIB

Matplotlib is a library in Python that creates charts, graphs and animation to help you visualize your data. Matplotlib is part of the standard Python Data Stack (pandas, NumPy, matplotlib, Jupyter). It has terrific integration with many other Python libraries. Pandas use matplotlib as a backend to help visualize data in Dataframes.

Matplotlib Object Hierarchy:

A plot is a hierarchy of nested Python objects. A hierarchy means a tree-like structure of matplotlib objects underlying each plot.

A Figure object is the outermost container on the matplotlib graphic which contains multiple Axes objects. Inside the axes there are multiple small objects like tick marks, individual lines, legends and text boxes.

Axes contains a region for plotting data and usually includes 2 or 3 (in case of 3D) Axis objects. Each Axes also has a title, an x-label and y-label. Axis objects set the scale and limits and generate ticks and tick labels.

X label and Y label are the text labels along x-axis and y-axis respectively.


Using Matplotlib you can create various plots such as:

·        Line Plot  

·        Bar graph  

·        Scatter plot

·        Pie plot and many more

  

Let’s get started by importing the necessary libraries as shown below:


Given below is an example of a simple line chart using plt.plot() function. Draw a line in diagram from position (0,0) to position (5, 200)

Labelling Axes:

We can label the axes using plt.xlabel for x axis plt.ylabel for y axis and plt.title to display the title of the graph.


Different customization methods for Line plot using Format Argument and Keyword Argument:

A line plot can be formatted using various arguments. You can format the axis labels, titles, colors and line styles. In the following example, function plt.plot defines ‘r’ as the format argument or format string and color=’green’ as keyword argument. When a conflict between format string and keyword argument arise keyword argument takes precedence.


Additional formatting techniques for a Line Graph: The Line graph can be formatted using various formatting techniques like linewidth, linestyle, markersize, fontstyle etc.

  

BAR Graph: Using matplotlib, we can also plot a bar graph. The following graph displays the number of students per year. The function plt.bar(year, students) is used to represent the data in a bar diagram.


The second bar graph shows the distribution of boys and girls from Grade 1 to Grade 4. The direction of the bar graph is changed to horizontal using kind=”barh”.

Scatter plot:


Here the above code generates a scatter plot with 2 sets of data points each with different colors and markers. The first plt.scatter() creates a scatter plot with a as the x-axis and b as the y-axis. Similarly second plt.scatter() creates a scatter plot with a and c. c=’blue’ and s=50 specifies the color and size respectively. Plt.legend() creates a legend for the plot with labels b and x corresponding to the first and second scatter plots respectively.


Pie Plot:

The following pie chart gives the percentage distribution of the students’ favorite subjects. The plt.pie() function is used to create pie plots in python. It contains the values 1,2,3,3 and 5 that represents the sizes of the pie slices. Labels can be used to specify the subject names. Autopct shows the percentage values with no decimal places (%.0f%% indicates integers. Likewise, %.1f%% indicates single digit after the decimal point and so on). Explode includes values to push a particular pie slice outwards for emphasis. Here in this example the last slice is exploded to emphasize the most favorite subject.


Conclusion:

This blog post covers the fundamental concepts of creating basic plots using Matplotlib. For further reading, you can check out the official Matplotlib documentation or explore additional tutorials and resources online. Happy plotting!

20 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page