use*_*002 18 python matplotlib
我正在尝试在python中创建一个图形,并且使得相同的annonate文本将具有两种颜色,一半的annonate将是蓝色而另一半将是红色.
我认为代码解释了自己.我有3行1绿色与绿色annonate,1蓝色与蓝色annonate.
第三个是红色,它是地块1和地块2的总和,我希望它有半个蓝色和半个绿色.
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'))
可能吗?
gep*_*cel 10
您可以使用r'$\textcolor{blue}{e^{-x/5}} + \textcolor{green}{e^{-x/1}}$'来使文本变蓝,变绿。例如,使用您自己的代码:
该图像由以下代码生成。使用默认matplotlibrc设置在matplotlib v2.1.2中进行了测试。
import matplotlib as matplotlib
matplotlib.use('pgf')
matplotlib.rc('pgf', texsystem='pdflatex')  # from running latex -v
preamble = matplotlib.rcParams.setdefault('pgf.preamble', [])
preamble.append(r'\usepackage{color}')
from numpy import *
from matplotlib.pyplot import *
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,-25), 
         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=(25,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'$\textcolor{blue}{e^{-x/5}} + \textcolor[rgb]{0.0, 0.5, 0.0}{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'))
savefig('test.png')
主要是对您的代码进行了以下更改:
pgf后端。color在pgf.preamblexytext已更改。color='g'在TE第二注释实际上并没有使用象RGB(0,255,0)的纯的“绿”的颜色。\textcolor[rgb]{0.0, 0.5, 0.0}使它看起来相似。我不认为你可以在一个注释中有多种颜色,因为 - 据我所知 - annotate只需要一个文本对象作为参数,而文本对象只支持单一颜色.所以,据我所知,没有"原生"或"优雅"的方式自动执行此操作.
但是,有一种解决方法:您可以在图形中任意放置多个文本对象.所以我就是这样做的:
fig1 = figure()
# all the same until the 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")
我做的是:
color='black';fig1);annotate(最终成为你在两次调用中看到的值text);color='white',因此它不会干扰重叠文本的颜色.这是输出:

它不是很优雅,它需要一些绘图来微调位置,但它完成了工作.
如果您需要多个图表,也许有一种方法可以自动化放置:如果您不存储fig1对象,坐标将text成为图形中的实际x,y坐标 - 我发现有点难以使用,但也许它能让你使用注释的xy坐标自动生成它们吗?听起来对我来说不值得,但如果你做到了,我想看看代码.
| 归档时间: | 
 | 
| 查看次数: | 17842 次 | 
| 最近记录: |