matplotlib在同一个注释中有两种不同的颜色

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'))
Run Code Online (Sandbox Code Playgroud)

可能吗?

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')
Run Code Online (Sandbox Code Playgroud)

主要是对您的代码进行了以下更改:

  1. 您需要使用pgf后端。
  2. Usepackage colorpgf.preamble
  3. 第1个和第2个注释有些重叠,因此xytext已更改。
  4. color='g'在TE第二注释实际上并没有使用象RGB(0,255,0)的纯的“绿”的颜色。\textcolor[rgb]{0.0, 0.5, 0.0}使它看起来相似。


kad*_*adu 9

我不认为你可以在一个注释中有多种颜色,因为 - 据我所知 - 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")
Run Code Online (Sandbox Code Playgroud)

我做的是:

  1. 我设置了注释color='black';
  2. 我在位置0.5,0.5处创建了两个文本对象(意思是中心fig1);
  3. 我手动改变了位置,直到它们与生成的黑色文本大致重叠annotate(最终成为你在两次调用中看到的值text);
  4. 我设置了注释color='white',因此它不会干扰重叠文本的颜色.

这是输出:

多色带注释的图形

它不是很优雅,它需要一些绘图来微调位置,但它完成了工作.

如果您需要多个图表,也许有一种方法可以自动化放置:如果​​您不存储fig1对象,坐标将text成为图形中的实际x,y坐标 - 我发现有点难以使用,但也许它能让你使用注释的xy坐标自动生成它们吗?听起来对我来说不值得,但如果你做到了,我想看看代码.

  • 在绘制了这个之后,我意识到你的情节中有一个"+",我之前没有注意到.我确信这些说明足以让你用额外的`text`来绘制它.另请查看[此答案](http://stackoverflow.com/a/9185143/3540074),它提供了一种更优雅的方式来实现它,但我无法使其工作. (2认同)