w3resource

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!

List of Matplotlib Exercises:

Matplotlib Basics

Matplotlib: matplotlib-logo

Creating Plots

Figure

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

Axes

Operator Description
fig.add_axes()
a = fig.add_subplot(222)
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, May 2023 compared to a year ago:

`
Rank Change Language Share Trend
1 Python 27.27 % -0.5 %
2 Java 16.35 % -1.6%
3 Javascript 9.52 % +0.2%
4 C# 6.92 % -0.3%
5 C/C++ 6.55 % -0.4 %
6 PHP 5.1 % -0.5 %
7 R 4.34 % -0.2%
8 TypeScript 2.88 % +0.3%
9 up arrow Swift 2.3 % +0.1%
10 down arrow Objective-C 2.13% -0.1%
11 down arrow Rust2.08% +0.8%
12 up arrow Go 1.95% +0.4%
13 down arrow Kotlin 1.77 % +0.1%
14 down arrow Matlab 1.63 % -0.2%
15 up arrow Ruby 1.04% +0.2%
16 up arrow Ada 1.02 % +0.4 %
17 down arrow VBA 0.95 % +0.1 %
18 down arrow Powershell 0.95 % +0.2 %
19 down arrow Dart 0.78 % +0.1 %
20 down arrow Scala 0.78 % +0.2 %
21 Visual Basic 0.61 % +0.1 %
22 Lua 0.59 % +0.1 %
23 Abap 0.48 % +0.1 %
24 up arrow Cobol 0.4 % +0.2 %
25 Julia 0.4 % +0.0 %
26 down arrow Groovy 0.37 % -0.0 %
27 down arrow Perl 0.36 % +0.1 %
28 down arrow Haskell 0.32 % +0.1 %
29 Delphi/Pascal 0.16 % +0.1 %

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

TIOBE Index for May 2023

May 2023 May 2022 Change Programming Language Ratings Change
1 1 Python 13.45% +0.71%
2 2 C 13.35% +1.76%
3 3 Java 12.22% +1.22%
4 4 C++ 11.96% +3.13%
5 5 C# 7.43% +1.04%
6 6 Visual Basic 3.84% -2.02%
7 7 JavaScript 2.44% +0.32%
8 10 up arrow PHP 1.59% +0.07%
9 9 SQL 1.48% -0.39%
10 8 down arrow Assembly language 1.20% -0.72%
11 11 Delphi/Object Pascal 1.01% -0.41%
12 14 up arrow Go 0.99% -0.12%
13 24 up arrow Scratch 0.95% +0.29%
14 12 down arrow Swift 0.91% -0.31
15 20 up arrow MATLAB 0.88% +0.06%
16 13 down arrow R 0.82% -0.39%
17 28 up arrow Rust 0.82% +0.42%
18 19 up arrow Ruby 0.80% -0.06%
19 30 up arrow Fortran 0.78% +0.40%
20 15 down arrow Classic Visual Basic 0.75% -0.28%

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

List of Exercises with Solutions :



Follow us on Facebook and Twitter for latest update.




We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook