python - matplotlib two different colors in the same annotate -
i trying create figure in python , make same annonate text have 2 colors, half of annonate blue , other half red.
i think code explain itself. have 3 lines 1 green green annonate, 1 blue blue annonate.
the 3rd red summation of plot 1 , plot 2, , want have half annonate blue , half green.
ipython -pylab
x=arange(0,4,0.1) exp1 = e**(-x/5) exp2 = e**(-x/1) exp3 = e**(-x/5) +e**(-x/1) figure() plot(x,exp1) plot(x,exp2) plot(x,exp1+exp2) title('exponential decay') annotate(r'$e^{-x/5}$', xy=(x[10], exp1[10]), xytext=(-20,-35), textcoords='offset points', ha='center', va='bottom',color='blue', bbox=dict(boxstyle='round,pad=0.2', fc='yellow', alpha=0.3), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.95', color='b')) annotate(r'$e^{-x/1}$', xy=(x[10], exp2[10]), xytext=(-5,20), textcoords='offset points', ha='center', va='bottom',color='green', bbox=dict(boxstyle='round,pad=0.2', fc='yellow', alpha=0.3), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=-0.5', color='g')) annotate(r'$e^{-x/5} + e^{-x/1}$', xy=(x[10], exp2[10]+exp1[10]), xytext=(40,20), textcoords='offset points', ha='center', va='bottom', bbox=dict(boxstyle='round,pad=0.2', fc='yellow', alpha=0.3), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=-0.5', color='red'))
is possible?
i don't think can have multiple colours in single annotation, since—as far know—annotate
takes 1 text object parameter, , text objects support single colours. so, knowledge, there's no "native" or "elegant" way of automatically doing this.
there is, however, workaround: can have multiple text objects placed arbitrarily in graph. here's how i'd it:
fig1 = figure() # same until last "annotate": annotate(r'$e^{-x/5}$'+r'$e^{-x/1}$', xy=(x[10], exp2[10]+exp1[10]), xytext=(40,20), textcoords='offset points', ha='center', va='bottom',color='white', bbox=dict(boxstyle='round,pad=0.2', fc='yellow', alpha=0.3), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=-0.5', color='r')) fig1.text(0.365, 0.62, r'$e^{-x/5}$', ha="center", va="bottom", size="medium",color="blue") fig1.text(0.412, 0.62, r'$e^{-x/1}$', ha="center", va="bottom", size="medium",color="green")
what did was:
- i set annotation
color='black'
; - i created 2 text objects @ positions 0.5, 0.5 (which means center of
fig1
); - i manually changed positions until overlapping black text generated
annotate
(which ended being values see in 2 callstext
); - i set annotation
color='white'
, doesn't interfere colour of overlapping text.
here's output:
it's not elegant, , require plotting fine-tune positions, gets job done.
if need several plots, perhaps there's way automate placement: if don't store fig1
object, coordinates text
become actual x,y coordinates in graph—i find bit harder work with, maybe it'd enable generate them automatically using annotation's xy
coordinates? doesn't sound worth trouble me, if make happen, i'd see code.
Comments
Post a Comment