# 2. Plot types¶

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)
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #semilogEx.py import numpy as np import matplotlib.pyplot as plt # close all the figures, if open from previous commands plt.close('all') x=np.linspace(0.01, 5, 100) e=np.exp(x) #linear plot plt.plot(x,e) plt.xlabel("x") plt.ylabel("y=exp(x)") plt.title("Linear Y axis") #semilog plot #log(exp(x))=x therefore straight line will be displayed plt.figure() plt.semilogy(x,e) #semilogy: semilog y-axis plt.xlabel("x") plt.ylabel("y=exp(x)") plt.title("Log Y axis") plt.show() #display the 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.

Listing 2.2 Histogram, Fig. 2.3
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #histogramEx.py import numpy as np import matplotlib.pyplot as plt plt.close("all") ur = np.random.rand(10000) nr = np.random.randn(10000) plt.subplot(2,2,1) plt.hist(ur) plt.xlabel("Uniform Random Number, Default 10 Bin") plt.subplot(2,2,2) plt.hist(ur, 20) # display 20 bins plt.xlabel("Uniform Random Number, 20 Bin") plt.subplot(2,2,3) plt.hist(nr) plt.xlabel("Normal Random Number, Default 10 Bin") plt.subplot(2,2,4) plt.hist(nr, 20) # display 20 bins plt.xlabel("Normal Random Number, 20 Bin") plt.show() 

## 2.3. Scatter plot¶

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.

Listing 2.3 Scatter plot, Fig. 2.4
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #scatterEx.py import numpy as np import matplotlib.pyplot as plt plt.close("all") N=100 x = np.linspace(0, 2*np.pi, N) noise = np.random.randn(N) signal = 2*np.sin(x) y = signal + noise plt.plot(x, signal) # signal + noise plt.scatter(x, y) #scatter plot plt.figure() y = signal + 0.2*noise # singal + 0.2*noise i.e. low noise plt.plot(x, signal) plt.scatter(x, y) #scatter plot plt.show() 

## 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.4 Pie chart, Fig. 2.5
  1 2 3 4 5 6 7 8 9 10 11 12 #pieEx.py import matplotlib.pyplot as plt plt.close("all") x = [30, 20, 15, 25, 10] dataName = ['data1', 'data2', 'data3', 'data4', 'data5'] plt.figure(figsize=(5,5)) #figsize: output figure window size plt.pie(x, labels=dataName) plt.show() 
Listing 2.5 Pie chart: explode and auto-percentage, Fig. 2.6
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 #pieEx2.py import matplotlib.pyplot as plt plt.close("all") x = [10, 10, 20, 20, 30] dataName = ['data1', 'data2', 'data3', 'data4', 'data5'] explode = [0.01, 0, 0, 0.04, 0.09] plt.figure(figsize=(5,5)) # autopct='%.2f %%': %.2f display value upto 2 decimal, # %% is used for displaying % at the end plt.pie(x, explode=explode, labels=dataName, autopct='%.2f%%') plt.show() 

## 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.

Listing 2.6 Polar plot, Fig. 2.7
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 #polarplotEx.py import numpy as np import matplotlib.pyplot as plt plt.close("all") x=np.linspace(0,2*np.pi, 1000) # polar axes is based on clipping so that r >= 0. # therefore only 2 lobes are shown as oppose to 4 lobes. y = np.cos(2*x) plt.polar(x, y) plt.show() Fig. 2.7 Matplotlib: polar plot of Cos(2x) 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.

Listing 2.7 Bar Chart, Fig. 2.9
  1 2 3 4 5 6 7 8 9 10 11 #barchartEx.py import matplotlib.pyplot as plt plt.close("all") x = [1, 2, 4] years = [1999, 2014, 2030] plt.bar(years, x) plt.show() 
• 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.

Listing 2.8 Bar Chart with multiple data, Fig. 2.10
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #barchartCombineEx.py import numpy as np import matplotlib.pyplot as plt plt.close("all") #### subplot 1 A = [2, 4, 8, 6] increment = [5, 6, 4, 1] years = [2005, 2010, 2015, 2020] plt.subplot(2,1,1) plt.bar(years, A, color = 'b', label='A') plt.bar(years, increment, color = 'r', bottom = A, label='increment') plt.legend() plt.show() ##### subplot 2 x = [1, 2, 4] y = [3.5, 3, 2] z = [2, 3, 1.5] width = 0.2 locs = np.arange(1, len(x)+1) plt.subplot(2,1,2) plt.bar(locs, x, width=width, label='x') plt.bar(locs+width, y, width=width, color="red", label='y') plt.bar(locs+2*width, z, width=width, color="black", label='z') plt.legend() plt.xticks([1.25, 2.25, 3.25], [r'$2000$', r'$2005$', r'$2010$']) plt.show()