2 Plots Python

  • Related Questions & Answers
  • Selected Reading
PythonServer Side ProgrammingProgramming

Python has the ability to create graphs by using the matplotlib library. It has numerous packages and functions which generate a wide variety of graphs and plots. It is also very simple to use. It along with numpy and other python built-in functions achieves the goal. In this article we will see some of the different kinds of graphs it can generate.

Simple Graphs

Here we take a mathematical function to generate the x and Y coordinates of the graph. Then we use matplotlib to plot the graph for that function. Here we can apply labels and show the title of the graph as shown below. We are plotting the graph for the trigonometric function − tan.

Example

Python 2 plots in one figure

Output

Running the above code gives us the following result −

Multiplots

Multiple plots python

Box plot elements (Image by Author / Rizky MN). In Figure 2, you can see there is an outlier. How can the box plot detect the outliers? An outlier is detected in the box plot when its value is smaller than Q1–1.5 x IQR or larger than Q3+1.5 x IQR. Before I create a box plot with Matplotlib, I will generate the mock data using this code. The line chart is used to display the information as a series of the line.

We can have two or more plots on a single canvas by creating multiple axes and using them in the program.

2 axis python

It must plot onto the “currently active” matplotlib Axes. This will be true of functions in the matplotlib.pyplot namespace, and you can call matplotlib.pyplot.gca to get a reference to the current Axes if you want to work directly with its methods. It must accept the data that it plots in positional arguments. A compilation of the Top 50 matplotlib plots most useful in data analysis and visualization. This list helps you to choose what visualization to show for what type of problem using python's matplotlib and seaborn library.

Example

Output

Running the above code gives us the following result −

Grid of Subplots

Python 2 Plots In One Figure

We can also create a grid containing different graphs each of which is a subplot. For this we use the function subplot2grid. Here we have to choose the axes carefully so that all the subplots can fit in to the grid. A little hit an dtrail may be needed.

Example

Output

Running the above code gives us the following result:

Contour Plot

Contour plots (sometimes called Level Plots) are a way to show a three-dimensional surface on a two-dimensional plane. It graphs two predictor variables X Y on the y-axis and a response variable Z as contours.Matplotlib contains contour() and contourf() functions that draw contour lines and filled contours, respectively.

Example

Output

Running the above code gives us the following result:

In this chapter, various plot types are discussed.

2.1. Semilog Plot¶

Semilog plots are the plots which have y-axis as log-scale and x-axis as linear scale as shown in Fig. 2.2. Listing 2.1 plots both the semilog and linear plot of the function (e^x).

Listing 2.1 Linear plot (Fig. 2.1) vs Semilog plot (Fig. 2.2)

Fig. 2.1 Linear Plot

2.2. Histogram¶

Histogram can be generated using hist() command as illustrated in line 11 in Listing 2.2. By default it generates 10 bins, which can be increased by providing the number of bins as shown in line 15. Further from Fig. 2.3, we can see that ‘rand’ generates the random number in the range [0,1] with uniform density, whereas ‘randn’ generates the random number in the range [-1,1] with Gaussian (Normal) density.

2.3. Scatter plot¶

Python

Scatter plots are similar to simple plots and often use to show the correlation between two variables. Listing 2.3 generates two scatter plots (line 14 and 19) for different noise conditions, as shown in Fig. 2.4. Here, the distortion in the sine wave with increase in the noise level, is illustrated with the help of scatter plot.

2.4. Pie chart¶

Here, pie charts are generated in two formats. Listing 2.4 generates a simple Pie chart with data names as show in Fig. 2.5. Also in line 9, figsize=(5,5) command is used here, to resize the output figure window. Further, Listing 2.5 adds additional features (line 13) to it i.e. explode and auto-percentage as shown in Fig. 2.6.

Listing 2.5 Pie chart: explode and auto-percentage, Fig. 2.6

Fig. 2.5 Pie Chart with labels, Listing 2.4

2 Axis Python

2.5. Polar plot¶

In matplotlib, polar plots are based on clipping of the curve so that $rge0$. For example, in Fig. 2.7 (generated by line 12 in Listing 2.6), only two lobes of (cos(2x)) are generated instead of four. Other two lobes have negative value of ‘r’, therefore these are clipped by the matplotlib. The actual four lobes are shown in Fig. 2.8, which can not be generated by matplotlib.

Fig. 2.8 Actual: polar plot of Cos(2x)

2.6. Bar chart¶

In this section, two types of bar charts are discussed. Listing 2.7 plots a simple bar chart for ‘years vs x’; whereas Listing 2.8 plots multiple data.

  • Explanation Listing 2.8

    In Fig. 2.10, the data ‘increment’ is plotted above the data ‘A’ using ‘bottom’ parameter in line 14.

    Further, in the lower subplot, bar charts are plotted side by side using combination of ‘locs’ and ‘width’ variable in line 24 and 25. ‘width’ parameter set the width of the bar; which is set to 0.2 in line 24. Line 27 plots the data ‘x’ first; then, line 28 plots next data set i.e. ‘y’, but location is shifted by the ‘width’ due to command ‘locs+width’ in the line. Hence, bar chart is plotted beside the bars of the line 27. After that, line 29 shifted the plot of data ‘z’ by ‘2*width’. Finally line 32 add ticks for the x-axis and we get the final plot as shown in Fig. 2.10.

Python Plot 2 Figures

Listing 2.8 Bar Chart with multiple data, Fig. 2.10

Python Plot 2 Plots

Fig. 2.10 Bar chart with multiple data, Listing 2.8