Data Visualization for absolute beginners[part 3/3]

Legends, labels, and titles

Ishank Sharma
Analytics Vidhya
5 min readJun 15, 2020

--

By now we have covered the basics of creating a figure canvas and add axes instances to it, Let’s now focus on how we can add titles, axis labels, and legends to our plots.

Figure titles

An axes contains the method set_titlewhich can be added to each axis instance in a figure.

Axis labels

Similarly, for setting xlabel and ylabel we use set_xlabel and set_ylabelrespectively.

Legends

We will use label = “label text” keyword when plots are added to the figure, and then we are going to call legend() method without argument for adding it to the figure.

Observe and see how the legend overlaps some of the actual plot!

We should note that the legend function takes the optional argument loc that is used to specify where the legend will be drawn.
See the documentation for more details

Setting colors, linewidths, linetypes

Matplotlib provides us a bunch of options for customizing colors, linewidths, and linetypes. These are used to change how our plot looks.

Colors with MatLab like syntax

Using matplotlib we will now define the color of lines in multiple ways. For eg ‘g — ’ means a green dashed line.

Colors with the color= parameter

Another way to define colors is by their RGB or HEX codes we can also provide alpha value to indicate the opacity.

Line and marker styles

Changing the line width can be done using linewidth or lw keyword. The line style can be selected using ls orlinestyle keyword. Linestyle defines the line type such as dashed line or a dotted line.

Control over axis appearance

Now we will be learning about controlling the axis sizing properties in a figure.

Plot range

To add ranges of the axes we can use set_ylim and set_xlim attributes to the axis object, or axis('tight') for automatically getting "tightly fitted" axes ranges.

Special Plot Types

The matplotlib is not just restricted to these simple line plots, it also provides us many specialized plots that we can create such as bar plots, pie charts, scatter plots and much more.

But in my opinion it’s better to use seaborn for these special types of charts. And we will discuss about them in a different tutorial.

Before wrapping up let’s discuss a few more examples of these specialtype of plots.

Scatter Plot

Histogram

Rectangular Box Plot

This wraps up the fundamental of this beautiful python library, and I hope you enjoyed learning it as much as I did while making this tutorial.

Congratulations! on completing part 3/3 of the series🎉🎉🎉

I will soon add more visualisation tutorials with slightly advanced libraries such as seaborn and plotly where we will be able to visualise more complex data.
Thanks for reading🙂

Further Readings

Sources

--

--

Ishank Sharma
Analytics Vidhya

Trying to bring easy access to knowledge + hacks for everyone.