# Matplotlib: - Exercises, Practice, Solution

[An editor is available at the bottom of the page to write and execute the scripts.]

Matplotlib is a Python plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in Python scripts, the Python and IPython shells, the Jupyter notebook, web application servers, and four graphical user interface toolkits.

The best way we learn anything is by practice and exercise questions. Here you have the opportunity to practice the NumPy concepts by solving the exercises starting from basic to more complex exercises. A sample solution is provided for each exercise. It is recommended to do these exercises by yourself first before checking the solution.

Hope, these exercises help you to improve your Matplotlib coding skills. Currently, following sections are available, we are working hard to add more exercises .... Happy Coding!

Matplotlib Basics

Creating Plots

Figure

Operator Description
fig = plt.figures() a container that contains all plot elements

Axes

Operator Description
Initializes subplot
A subplot is an axes on a grid system row-col-num.
fig, b = plt.subplots(nrows=3, nclos=2) Adds subplot
ax = plt.subplots(2, 2) Creates subplot

Plotting

1D Data

Operator Description
lines = plt.plot(x,y) Plot data connected by lines
plt.scatter(x,y) Creates a scatterplot, unconnected data points
plt.bar(xvalue, data , width, color...) simple vertical bar chart
plt.barh(yvalue, data, width, color...) simple horizontal bar
plt.hist(x, y) Plots a histogram
plt.boxplot(x,y) Box and Whisker plot
plt.violinplot(x, y) Creates violin plot
ax.fill(x, y, color='lightblue')
ax.fill_between(x,y,color='yellow')
Fill area under/between plots

2D Data

Operator Description
fig, ax = plt.subplots()
im = ax.imshow(img, cmap, vmin...)
Colormapped or RGB arrays

Saving plots

Operator Description
plt.savefig('pic.png') Saves plot/figure to image
plt.savefig('transparentback.png') Saves transparent plot/figure to image

Customization

Color

Operator Description
plt.plot(x, y, color='lightblue')
plt.plot(x, y, alpha = 0.4)
colors plot to color blue
plt.colorbar(mappable,
orientation='horizontal')
mappable: the Image, Contourset etc to which colorbar applies

Markers

Operator Description
plt.plot(x, y, marker='*') adds * for every data point
plt.scatter(x, y, marker='.') adds . for every data point

Lines

Operator Description
plt.plot(x, y, linewidth=2) Sets line width
plt.plot(x, y, ls='solid') Sets linestyle, ls can be ommitted, see 2 below
plt.plot(x, y, ls='--') Sets linestyle, ls can be ommitted, see below
plt.plot(x,y,'--', x**2, y**2, '-.') Lines are '--' and '_.'
plt.setp(lines,color='red',linewidth=2) Sets properties of plot lines

Text

Operator Description
plt.text(1, 1,'Example
Text',style='italic')
Places text at coordinates 1/1
ax.annotate('some annotation', xy=(10, 10)) Annotate the point with coordinatesxy with text s
plt.title(r'\$delta_i=20\$', fontsize=10) Mathtext

Limits

Operators Description
plt.xlim(0, 7) Sets x-axis to display 0 - 7
other = array.copy() Creates deep copy of array
plt.ylim(-0.5, 9) Sets y-axis to display -0.5 - 9
ax.set(xlim=[0, 7], ylim=[-0.5, 9])
ax.set_xlim(0, 7)
Sets limits
plt.margins(x=1.0, y=1.0) Set margins: add padding to a plot, values 0 - 1
plt.axis('equal') Set the aspect ratio of the plot to 1

Legends/Labels

Operator Description
plt.title('just a title') Sets title of plot
plt.xlabel('x-axis') Sets label next to x-axis
plt.ylabel('y-axis') Sets label next to y-axis
ax.set(title='axis', ylabel='Y-Axis', xlabel='X-Axis') Set title and axis labels
ax.legend(loc='best') No overlapping plot elements

Ticks

Operator Description
plt.xticks(x, labels, rotation='vertical') Set ticks
ax.xaxis.set(ticks=range(1,5), ticklabels=[3,100,-12,"foo"]) Set x-ticks
ax.tick_params(axis='y', direction='inout', length=10) Make y-ticks longer and go in and out

## Popularity of Programming Language Worldwide, Jan 2021 compared to a year ago:

Rank Change Language Share Trend
1 Python 30.44 % +1.2 %
2 Java 16.76 % -2.0 %
3 Javascript 8.44 % +0.3 %
4 C# 6.53 % -0.7%
5 C/C++ 6.33 % +0.3 %
6 PHP 6.05 % -0.2 %
7 R 3.87 % +0.1 %
8 Objective-C 3.71 % +1.2 %
9 Swift 2.14 % -0.3 %
10 TypeScript 1.78 % -0.0 %
11 Matlab 1.74 % -0.1 %
12   Kotlin 1.7 % +0.0 %
13 Go 1.33 % +0.1 %
14 VBA 1.2 % -0.2 %
15 Ruby 1.12 % -0.2 %
16 Rust 1.03 % +0.3 %
17 Scala 0.72 % -0.3 %
18 Visual Basic 0.69 % -0.2 %
19 Lua 0.64 % +0.3 %
20 Ada 0.63 % +0.3 %
21 Dart 0.56 % +0.1 %
22 Abap 0.51 % +0.0 %
23 Perl 0.48 % -0.1 %
24 Julia 0.38 % +0.1 %
25 Groovy 0.37 % -0.1 %
26 Cobol 0.35 % +0.0 %
27 Haskell 0.27 % -0.0 %
28 Delphi 0.26 % +0.0 %

Source : http://pypl.github.io/PYPL.html

TIOBE Index for January 2021

Jan 2021 Jan 2020 Change Programming Language Ratings Change
1 2 C 17.38% +1.61%
2 1 Java 11.96% -4.93%
3 3 Python 11.72% +2.01%
4 4 C++ 7.56% +1.99%
5 5 C# 3.95% -1.40%
6 6 Visual Basic 3.84% -1.44%
7 7 JavaScript 2.20% -0.25%
8 8 PHP 1.99% -0.41%
9 18 R 1.90% +1.10%
10 23 Groovy 1.84% +1.23%
11 15 Assembly language 1.64% +0.76%
12 10 SQL 1.61% +0.10%
13 9 Swift 1.43% -0.36%
14 14 Go 1.41% +0.51%
15 11 Ruby 1.30% +0.24%
16 20 MATLAB 1.15% +0.41%
17 19 Perl 1.02% +0.27%
18 13 Objective-C 1.00% +0.07%
19 12 Delphi/Object Pascal 0.79% -0.20%
20 16 Classic Visual Basic 0.79% -0.04%

Source : https://www.tiobe.com/tiobe-index/

List of Exercises with Solutions :

﻿