天体物理模型中箭头连接方式的问题

2024-10-03 19:21:47 发布

您现在位置:Python中文网/ 问答频道 /正文

用这段代码我试图绘制这样的图像:this image is made by powerpoint]1但是我无法得到它,我得到了这一个:created through code。在python的箭头连接风格方面,有谁能帮我吗。我想问的最重要的一点是,我怎样才能像我在图片1上写的那样把箭头转到特定的方向并在上面写字

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)): for n in np.arange(len(N)) : 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 xx = np.transpose(np.loadtxt(''+link+'/'+ratio[r1]+'.txt')) yy = np.transpose(np.loadtxt(''+link+'/'+ratio[r2]+'.txt')) for m in np.arange(len(ionz)): for f in np.arange(len(fesc)): if fesc[f]>=1: plt.plot(yy[m,f], xx[m,f], 'o-', color= ''+str(grey[f])+'') if fesc[f]<1: 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"), ) 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()

Tags: inimportforagelentimenpplt
1条回答
网友
1楼 · 发布于 2024-10-03 19:21:47

annotation参数在箭头开始处获取文本,并指向要素(此信息位于“annotate”的matplotlib文档中)

要使文本跟随箭头,需要创建一个单独的文本框对象,该对象是旋转的(https://matplotlib.org/examples/pylab_examples/text_rotation.html)。您仍然可以使用annotate来创建箭头,但现在在开头有一个空字符串

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.

请注意,旋转是从水平方向开始的,逆时针方向,以度为单位-文本旋转链接有一些很好的视觉示例

相关问题 更多 >