2014-06-08 100 views
13

我想在python中创建一个图形,并使得相同的annonate文本将具有两种颜色,annonate的一半将是蓝色,另一半将是红色。matplotlib两种不同的颜色在相同的注释

我认为代码可以解释它自己。我有3行1绿色与绿色annonate,1蓝色与蓝色annone。

第三个是红色的情节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')) 

这可能吗?

+0

@bli你看看[这个问题](https://stackoverflow.com/questions/9169052/partial-coloring-of-text-in-matplotlib)? – ImportanceOfBeingErnest

+0

谢谢。这个答案确实非常相关:https://stackoverflow.com/a/42768093/1878788 – bli

回答

7

我不认为您可以在单个注释中使用多种颜色,因为 - 据我所知 - 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") 

我所做的是:

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

下面是输出:

multi-colour annotated graph

这不是很优雅,而且它确实需要一些绘制微调的位置,但它能够完成任务。

如果你需要几个图,也许有一种方法来自动化放置:如果​​你没有存储fig1对象,text的坐标将成为图中实际的x,y坐标 - 我发现有点困难使用,但也许它可以让你使用注释的坐标自动生成它们?对我来说这听起来不值得麻烦,但如果你做到了,我希望看到代码。

+2

在绘制这个图之后,我意识到在你的情节中有一个'+',我之前没有注意到它。我相信这些说明足以让你用一个额外的“文本”来绘制它。 也检查出[这个答案](http://stackoverflow.com/a/9185143/3540074),它提供了一种更优雅的方式来做到这一点,但我无法使它工作。 – kadu

1

您可以使用r'$\textcolor{blue}{e^{-x/5}} + \textcolor{green}{e^{-x/1}}$'将文本设为蓝色,半绿色。使用自己的代码,例如:

enter image description here

是由下面的代码生成的图像。使用默认的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') 

主要是你的代码有以下变化:

  1. 您需要使用pgf后端。
  2. Usepackage colorpgf.preamble
  3. 有一些重叠与第一和第二注释,所以xytext改变。
  4. 第二个注释中的color='g'实际上并没有使用像rgb的(0,255,0)这样纯粹的“绿色”颜色。 \textcolor[rgb]{0.0, 0.5, 0.0}使它看起来一样。
+0

谢谢,这个工程。在我自己的情况下,我在TeX中遇到了内存问题,可能是由于要在散点图中绘制很多点。我用''lualatex''解决了'texsystem'问题。 – bli

+0

@bli,是的,那行'matplotlib.rc('pgf',texsystem ='pdflatex')'应该被删除。我认为默认的tex系统也可以。 – gepcel

相关问题