2017-05-08 23 views
0

有了這段代碼,我想繪製這種圖像:this image is made by powerpoint] 1但我無法得到它,我得到這個:created through code。有沒有人可以幫助我在Python中的箭頭連接風格。那我要問最重要的事情,怎麼能我可以把箭頭在特定的方向,正如我已經寫在圖像上寫他們1在天體物理學模型的箭頭connectionstyle中的問題

from __future__ import division 
 
import numpy as np 
 
import fileinput 
 
import os 
 
import shutil 
 
import time 
 
import matplotlib.pyplot as plt 
 
from matplotlib import cm 
 

 

 
start_time = time.time() 
 
colors = ['red','blue','sienna','black', 'orange','olive' , 'green', 'brown',\ 
 
'coral', 'aqua', 'y', 'purple', 'orchid', 'c', 'peru', 'darkcyan',\ 
 
'm', 'darkgoldenrod', 'lime', 'teal', 'indianred', 'lawngreen', 'tan', 'navy', 'pink' ] 
 

 
#~ electron_density= [1] 
 
hden = [2] 
 
D = [ '15.0','15.50', '16.0', '16.50', '17.0', '17.10', '17.20', '17.30', '17.40', '17.50',\ 
 
'17.60', '17.70', '17.80', '17.90', '18.0', '18.10', '18.20', '18.30', '18.40', '18.50', \ 
 
'19.0', '19.50', '20.0', '20.50', '21.0'] #NHI 
 

 
ionz = ['-3.4768207','-3.1768207', '-2.9768207', '-2.7768207', '-2.4768207', '-2.1768207', '-1.9768207', '-1.7768207', '-1.4768207'] #ionization parameter U 
 
#~ ionz = ['-1.4768207'] 
 

 
#~ N = ["z001", "z002", "z003", "z004", "z006", "z008", "z010", "z020"] 
 
#~ N = ["z001", "z002", "z003", "z004", "z006", "z008"] 
 
#~ N = ["z001", "z002", "z003", "z004"] 
 
#~ N = ["z001", "z002"] 
 
N = ["z001"] 
 

 
#~ age = ["6.0", "6.3", "6.5", "6.7", "6.9", "7.0", "7.3", "7.5", "7.7", "7.9", "8.0", "8.3", "8.5", "8.7", "8.9" ] 
 
#~ age = ["7.0", "7.3", "7.5", "7.7", "7.9", "8.0", "8.3", "8.5", "8.7", "8.9" ] 
 
age = ["6.0"] 
 
#~ 
 
colors = ['red','blue','sienna', 'orange', 'yellow' ,'lime', 'brown',\ 
 
'coral', 'aqua', 'y', 'purple', 'black','orchid', 'olive' ,'c', 'peru', 'darkcyan',\ 
 
'm', 'darkgoldenrod', 'pink', 'teal', 'indianred', 'lawngreen', 'tan', 'navy', 'green' ] 
 
grey = np.linspace(0,1,25) 
 

 
ratio = [ 'He2Hb_ratio','He2Hb_4686_ratio', 'C4C3_ratio', 'Si4Hb_ratio'] 
 
#~ fesc = [99, 99, 98, 95, 86, 83, 80, 75, 70, 63, 56, 48, 40, 31, 24, 18, 13, 9, 7, 4.0, 0.3, 0.0, 0.0, 0.0, 0.0] 
 
fesc = [99, 99, 98, 95, 86, 83, 80, 75, 70, 63, 56, 48, 40, 31, 24, 18, 13, 9, 7, 4.0, 0.3, 0.0, 0.0, 0.0, 0.0] 
 

 
r1 = 3 
 
r2 = 1 
 

 
#~ ~~~~~~~~~~~~~~~~~~~~~~~~~~Ionization parameter~~~~~~~~~~~~~~~~~~~~~~~ 
 
for a in np.arange(len(age)): 
 
\t for n in np.arange(len(N)) : 
 
\t \t link = '/home/evol/gsharma/cloudy_work/for_report/final_fig_codes/data/hden_'+str(hden[0])+'/line_ratios/age_'+age[a]+'_'+N[n]+'' #func of NHI_U 
 
\t \t xx = np.transpose(np.loadtxt(''+link+'/'+ratio[r1]+'.txt')) 
 
\t \t yy = np.transpose(np.loadtxt(''+link+'/'+ratio[r2]+'.txt')) 
 
\t \t for m in np.arange(len(ionz)): 
 
\t \t \t for f in np.arange(len(fesc)): 
 
\t \t \t \t if fesc[f]>=1: 
 
\t \t \t \t \t plt.plot(yy[m,f], xx[m,f], 'o-', color= ''+str(grey[f])+'') 
 

 
\t \t \t \t if fesc[f]<1: 
 
\t \t \t \t \t plt.plot(yy[m,f], xx[m,f], 'o-', color= ''+colors[m]+'') 
 

 

 
plt.annotate("increasing U", 
 
      xy=(-3.6,-1.1), xycoords='data', 
 
      xytext=(-3.7, -2.9), textcoords='data', 
 
      arrowprops=dict(arrowstyle="->", 
 
          connectionstyle="arc"), 
 
      ) 
 
      \t 
 
plt.xlabel('log([SiIV]_1393/H_beta)') 
 
plt.ylabel('log([HeII]_4686/H_beta)') 
 
plt.title('SiIV/Hbeta Vs HeII/Hbeta line ratios \n fesc_U::\ hden_'+str(hden[0])+', fix age , fix Z ') 
 
plt.grid(True) 
 
plt.show()

回答

1

註釋參數採用文本在開始的箭頭並指向該功能(此信息位於註釋的matplotlib文檔中)。

要讓文字跟在箭頭後面,您需要製作一個單獨的文字框對象(https://matplotlib.org/examples/pylab_examples/text_rotation.html)。您仍然可以使用註釋來創建一個箭頭,但現在在開始時使用空字符串。

plt.text(-3.8, -1.3, 'Increasing U', rotation = 79) 

plt.annotate("", 
      xy=(-3.5, -0.9), xycoords='data', 
      xytext=(-3.8,-3.3), textcoords='data', 
      arrowprops=dict(arrowstyle="->", 
          connectionstyle="arc"), 
      ) 

Here's a picture of the text and arrow above.

注意,旋轉從水平開始,逆時針方向,單位爲度 - 文本旋轉鏈接有一些很好的視覺例子。

+0

謝謝@tautastic,它的工作:) –