2019年7月12日 星期五

[ Python 文章收集 ] Matplotlib - HOW TO PLOT SUBPLOTS OF UNEQUAL SIZES

Source From Here 
Preface 
Sometimes we would like to focus more on some data and less on others, but still provide a visual display. The matplotlib function gridspec allows subplots of unequal size to be plotted on the same figure.How this function can be applied will be demonstrated using simulated data. Let’s simulate some common probability distributions of different statistics using Python’s numpy.random module: 
  1. #  
  2. 0) Initialization & Import libraries  
  3. #   
  4. import numpy as np  
  5. import matplotlib.pyplot as plt  
  6. import matplotlib.gridspec as gridspec  
  7. %matplotlib inline  
  8.   
  9. #  
  10. 1) Generate data  
  11. #   
  12. dist_norm = np.random.normal(loc=0, scale=1, size=1000)  
  13. dist_tdis = np.random.standard_t(df=29, size=1000)  
  14. dist_fdis = np.random.f(dfnum=59, dfden=28, size=1000)  
  15. dist_chsq = np.random.chisquare(df=2, size=1000)  
Plot Subplots Of Unequal Sizes 
Now we can plot these data in a single figure, which will have 1 large subplot on the left, and a column of 3 small subplots on the right. The code to generate subplots is long but repetitive. You will get the hang of how to specify different parameters quickly: 
  1. #  
  2. 2) Drawing plot  
  3. #   
  4.   
  5. # Plot figure with subplots of different sizes  
  6. fig = plt.figure(1)  
  7. # set up subplot grid  
  8. gridspec.GridSpec(3,3)  
  9.   
  10. # large subplot  
  11. plt.subplot2grid((3,3), (0,0), colspan=2, rowspan=3)  
  12. plt.locator_params(axis='x', nbins=10)  
  13. plt.locator_params(axis='y', nbins=5)  
  14. plt.title('Normal distribution')  
  15. plt.xlabel('Data values')  
  16. plt.ylabel('Frequency')  
  17. plt.hist(dist_norm, bins=30, color='0.30')  
  18.   
  19. # small subplot 1  
  20. plt.subplot2grid((3,3), (0,2))  
  21. plt.locator_params(axis='x', nbins=5)  
  22. plt.locator_params(axis='y', nbins=5)  
  23. plt.title('t distribution')  
  24. plt.xlabel('Data values')  
  25. plt.ylabel('Frequency')  
  26. plt.hist(dist_tdis, bins=30, color='b')  
  27.   
  28. # small subplot 2  
  29. plt.subplot2grid((3,3), (1,2))  
  30. plt.locator_params(axis='x', nbins=5)  
  31. plt.locator_params(axis='y', nbins=5)  
  32. plt.title('F distribution')  
  33. plt.xlabel('Data values')  
  34. plt.ylabel('Frequency')  
  35. plt.hist(dist_fdis, bins=30, color='r')  
  36.   
  37. # small subplot 3  
  38. plt.subplot2grid((3,3), (2,2))  
  39. plt.locator_params(axis='x', nbins=5)  
  40. plt.locator_params(axis='y', nbins=5)  
  41. plt.title('Chi-square distribution')  
  42. plt.xlabel('Data values')  
  43. plt.ylabel('Frequency')  
  44. plt.hist(dist_chsq, bins=30, color='g')  
  45.   
  46. # fit subplots and save fig  
  47. fig.tight_layout()  
  48. fig.set_size_inches(w=11,h=7)  
  49. fig_name = 'plot.png'  
  50. fig.savefig(fig_name)  
Line 6. Create a figure object called fig so we can refer to all subplots in the same figure later. 

Line 8. Call the function gridspec.Gridspec and specify an overall grid for the figure (in the background). Here, give the figure a grid of 3 rows and 3 columns. 

Line 11. Call the function plt.subplot2grid() and specify the size of the figure’s overall grid, which is 3 rows and 3 columns (3,3). Specify the location of the large subplot: start counting from row 0 column 0 (0,0) and make a subplot across 2 columns and 3 rows colspan=2, rowspan=3. (Remember, Python indexes from 0, so the 3 rows or columns will be indexed as row or column 0, 1, 2.

Lines 12-17. In this subplot, do the following: 
* for the x and y axes, set the number of bins to maximum of 10 and 5
* give the plot a title
* give the x and y axes titles
* plot a histogram of the data with 30 bins and set the colour

Line 47. Give the figure a tight_layout so that subplots are nicely spaced between each other. (Ironically, if you don’t specify this, the subplots are squeezed together even more tightly and text is overlaid.

Lines 48-50. Set figure size, give it a name and save the figure 

Play around with different parameter settings for each of the distributions to see how these change the properties of the distribution. The code above produces Figure 1 below: 


Summary 
In Python’s matplotlib library, the function gridspec can be applied to plot subplots of unequal sizes by specifying an overall row and column grid for a figure, then referencing location and size of individual subplots within the figure. 

Note: Another way to plot subplots of unequal sizes in Matplotlib is to specify subplot parameters in a style similar to Matlab’s plotting style. Check out this tutorial.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...