Matplotlib tutorial with Python

 

Bar plots:

They are used to represent comparison between the data points using the bars either horizontally or vertically

Matplotlib 1 (i2tutorials)

 

 

Basic plotting:

 

Matplotlib 2 (i2tutorials)

 

Matplotlib 3 (i2tutorials)

 

Color change:

Let us make few changes like figure size, color of lines, line width etc

using the function “figure”, “plot”

In the figure function we can provide the size of the image output details and dpi value

which is dots per inch to increase visual quality.

In the plot function , we can provide the line color and width for cos and sin curves.

 

 

Note:

Execute above 2 lines combined to see the output.

 

Matplotlib 4 (i2tutorials)

Ticks:

In Matplotlib, ticks are generally displayed as small marks on both X and Y axis reference points for plotting the curves.

You can observe in our previous topic, the plot has 7 ticks on x axis and 9 ticks on Y axis.We can change those ticks using the xticks() and yticks() functions.

 

 

Matplotlib 5 (i2tutorials)

Tick labels:

If you observe in the previous plot for ticks on X axis we have given -pi , 0 , pi which are represented in numerical format. But i want to print the labels as mathematical pi symbol.we have to use the latex to render the label as we required and we need provide those values in the second argument list of corresponding label.

 

 

Matplotlib 6 (i2tutorials)

 

Moving Spines:

Spines are the lines which required to place at arbitrary positions that are connecting the axis tick marks and noting the boundaries. So, we have to move the lines or the axis behind our sin, cos curves where the left ones will be the negative values and right ones will be the positive values for X axis. For Y axis, negatives will be the down and positives will be on the upper side of the axis.

 

 

Matplotlib 7 (i2tutorials)

Adding a legend:

In Matplotlib, legend is a function which is used to place the legend on the axes. we can place the legend in various positions and can be placed either inside or outside of the chart or plot.In our example, let us add the legend in the upper right corner.

 

Matplotlib 8 (i2tutorials)

Contour plot:

Contour is nothing but the outlines or shape of something.Contour plots are isolines to represent the graphical 3-D surface by plotting the Z slices. Simply we can say that Contour plots can be used to show 3-Dimensional surfaces on a 2-Dimensional plane.

 

Syntax:

– To create a contour plot of an array Z. The level values are taken automatically by itself. contour(Z)

– To create a contour plot using the coordinates X, Y specify the (x, y) coordinates of the surface. contour(X,Y,Z)

– To contour up to N automatically-chosen levels. contour(X,Y,Z,N)

– To draw contour lines at specified values in sequence V in increasing order. contour(Z,V) ,contour(X,Y,Z,V)

– To fill the len(V)-1 regions between the values in V in increasing order. contourf(…, V)

 

When we want to plot the numpy arrays which consists some data as images can be rendered using the function called imshow().

Matplotlib 9 (i2tutorials)

 

Matplotlib 10 (i2tutorials)

 

Gridlines:

Creating the gridlines in the plot gives us the visualization of coordinate points clearly.

matplotlib.pyplot.grid(b=None, which=’major’, axis=’both’, **kwargs)¶

You can always turn the axes grids on or off by passing the argument b which is a boolean value. Now let us add the grid lines to the quiver plot.

 

 

Matplotlib 11 (i2tutorials)

Pie plot:

Pie plot is a kind of graphical representation in which a circle is divided into slices, where each slice represents a numeric proportion of entire circle.

Syntax:

matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6,

shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None,

textprops=None, center=(0, 0), frame=False, rotatelabels=False, hold=None, data=None)

 

Matplotlib 12 (i2tutorials)

 

Quiver plot:

Quiver plot is used to display velocity vectors as arrows with components (u,v)

at the points (x,y).

 

Syntax:

#Axes.quiver(*args, data=None, **kw)

Calling with different arguements

– quiver(U, V, **kw)

– quiver(U, V, C, **kw)

– quiver(X, Y, U, V, **kw)

– quiver(X, Y, U, V, C, **kw)

– U, V – Represents the Arrow data.

– X, Y – Represents location of arrows.

– C – Represents the Color of arrows.

 

Matplotlib 13 (i2tutorials)

Regular plot:

In this tutorial, we will discuss all about how to create a regular plot in matplotlib

 

Below are the supported color abbreviations:

character   color

– ‘b’        blue

– ‘g’        green

– ‘r’        red

– ‘c’        cyan

– ‘m’        magenta

– ‘y’        yellow

– ‘k’        black

– ‘w’        white

 

Matplotlib 14 (i2tutorials)

Scatter plots:

Scatter plots are just like regular line graphs which uses the X and Y axes to plot the input data. It is used in situations where we want to show the correlation between one and another.

 

Syntax:

matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None,

vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=None, **kwargs)

Here, s represents the Marker Size and c represents the marker color.

 

 

Matplotlib 15 (i2tutorials)

Sub plots:

Using the subplots, we can divide an area into multiple subplots in a grid.

We can do this by specifying the number of rows and columns and the number of plot.Here x and y must have same first dimension otherwise we get error.

 

Axes function:

#Many people confuse here with axes with axis. Axes is different than axis in matplotlib. Axis represents the x-axis or the y-axis where as the axes means it is mostly like a subplots which will allow the placement of those plots in any location within the figure or plot. We can keep a small plot with in a bigger one or we can place two small plots side by side with in the figure.

Matplotlib 16 (i2tutorials)

 

Matplotlib 17 (i2tutorials).jpg