2018年10月25日 星期四

[ Python 常見問題 ] matplotlib - Python xticks in subplots

Source From Here 
Question 
If I plot a single imshow plot I can use: 
  1. fig, ax = plt.subplots()  
  2. ax.imshow(data)  
  3. plt.xticks( [41424],  [51525] )  
to replace my xtick labels. Now, I am plotting 12 imshow plots using 
  1. f, axarr = plt.subplots(43)  
  2. axarr[i, j].imshow(data)  
How can I change my xticks just for one of these subplots? I can only access the axes of the subplots with axarr[i, j]. How can I access plt just for one particular subplot? 

How-To 
There are two ways: 
1. Use the axes methods of the subplot object (e.g. ax.set_xticks and ax.set_xticklabels) or
2. Use plt.sca to set the current axes for the pyplot state machine (i.e. the plt interface).

As an example (this also illustrates using setp to change the properties of all of the subplots): 
  1. import matplotlib.pyplot as plt  
  2.   
  3. fig, axes = plt.subplots(nrows=3, ncols=4)  
  4.   
  5. # Set the ticks and ticklabels for all axes  
  6. plt.setp(axes, xticks=[0.10.50.9], xticklabels=['a''b''c'],  
  7.         yticks=[123])  
  8.   
  9. # Use the pyplot interface to change just one subplot...  
  10. plt.sca(axes[11])  
  11. plt.xticks(range(3), ['A''Big''Cat'], color='red')  
  12.   
  13. fig.tight_layout()  
  14. plt.show()  

沒有留言:

張貼留言

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