2017年8月8日 星期二

[ Python 常見問題 ] Matplotlib - Plot x-axis with first x-axis value labeled as 1

Source From Here 
Question 
I am trying to plot a vector using python and matplotlib. My problem is that in matplotlib.pyplot, the x-axis of my data starts with 0 and ends on 23. And in the graph the same is considered. 

What I want is that this axis starts with label 1 (it is related to the first y value, or value #0 in natural python indexing) and ends on 24 (related to the last y value, or value #23 in natural python indexing). I tried matplotlib.pyplot.xlimwith xmin=1, but the problem is that, this way, the first dimension (0) disappears in the graph, and the upper bound continues to be 23. I want it to be 24 and the first y value having its x value labeled as 1 (not 0). 

This solution is not working for me. I am trying to have the labels [1,24] in the x-axis of the graph instead of [0,23]. As I wrote, if I start with 1 in x axis using xlim=1 or set_xlim=1, the first y value (dimension 0 of the vector) is not shown in the graph. It starts with second y value (dimension 1 of the vector) and ends with the last value. I don't want it. Here is the source code I am using: 
  1. import matplotlib.pyplot as pp  
  2. import numpy as np  
  3.   
  4. a=np.array( [0.104781510.099095640.013198260.007432250.004837210.182024190.017320460.041535360.033179910.05362890.005854230.009298710.006293630.121806540.006077810.03752038,0.055474520.014590150.006049090.011324420.007103630.111594290.00799220.04198672])  
  5.   
  6. pp.xlabel('Dimension')   
  7. pp.ylabel('Importance')  
  8. ax=pp.subplot(111)  
  9. ax.set_xlim(124)  
  10. dim=np.arange(1,24,1);  
  11. ax.plot(a, 'ro', color='r',linewidth=1.0, label="Graph2")  
  12. pp.xticks(dim)  
  13. pp.grid()     
  14. pp.show()      
  15. pp.close()  
When I run the code, the resulting image is the image below: 
 

It is expected that the first y value will be shown in x=1 and the last in x=24. But Python indexing starts with 0, so, looks like the code is 'shifting' the values, starting in x=2 (or x=1 in python natural indexing). 

How-To 
You can get the result you want by using numpy.roll
Roll array elements along a given axis. Elements that roll beyond the last position are re-introduced at the first.

to shift the values you want from your original array onto the indices 1 to 23, and then append the final element of your original array so it is at index 24. The code would be: 
  1. import matplotlib.pyplot as pp  
  2. import numpy as np  
  3.   
  4. a=np.array( [0.104781510.099095640.013198260.007432250.004837210.182024190.017320460.041535360.033179910.05362890.005854230.009298710.006293630.121806540.006077810.03752038,0.055474520.014590150.006049090.011324420.007103630.111594290.00799220.04198672])  
  5.   
  6. pp.xlabel('Dimension')   
  7. pp.ylabel('Importance')  
  8. ax=pp.subplot(111)  
  9. ax.set_xlim(124)  
  10. dim=np.arange(1,25,1)  
  11. ax.plot(np.append(np.roll(a,1),a[23]), 'ro', color='r',linewidth=1.0, label="Graph2")  
  12. pp.xticks(dim)  
  13. pp.grid()     
  14. pp.show()      
  15. pp.close()  
and the resulting plot looks like: 
 

Note the change in the line 
  1. dim=np.arange(1,25,1)  
is necessary to plot your x-axis tick marks from 1 to 24.

沒有留言:

張貼留言

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