我正在尝试用python创建一个图形,make is使同一个annonate文本有两种颜色,一半是蓝色,另一半是红色。
我认为代码可以解释自己。我有三条线,一条绿色和绿色的annonate,一条蓝色和蓝色的annonate。
第三个是红色的,它是1号和2号地块的总和,我希望它有一半是安妮蓝一半是绿色。
伊普顿-派拉布
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'))
有可能吗?
您可以使用
r'$\textcolor{blue}{e^{-x/5}} + \textcolor{green}{e^{-x/1}}$'
将文本设置为半蓝色、半绿色。例如,使用您自己的代码:图像由以下代码生成。使用matplotlib v2.1.2和默认的
matplotlibrc
设置进行测试。主要是您的代码,有以下更改:
pgf
后端。pgf.preamble
中使用包color
xytext
被更改。color='g'
实际上没有使用像rgb的(0,255,0)那样的纯“绿色”。\textcolor[rgb]{0.0, 0.5, 0.0}
让它看起来很像。我不认为在一个注释中可以有多种颜色,因为据我所知-
annotate
只接受一个文本对象作为参数,而文本对象只支持单一颜色。所以,据我所知,没有“本地”或“优雅”的方式可以自动完成这项工作。但是,有一个解决方法:可以在图形中任意放置多个文本对象。所以我会这样做:
我所做的是:
color='black'
fig1
的中心)annotate
生成的黑色文本大致重叠(这最终是您在对text
的两次调用中看到的值)color='white'
,这样就不会影响重叠文本的颜色。输出如下:
它不是很优雅,它确实需要一些阴谋来微调位置,但它完成了工作。
如果您需要几个绘图,也许有一种方法可以自动放置:如果您不存储
fig1
对象,那么text
的坐标将成为图形中的实际x,y坐标-我发现这有点难处理,但也许它可以使您使用注释的xy
坐标自动生成它们?听起来不值得我费心,但如果你能做到,我想看看代码。相关问题 更多 >
编程相关推荐