繁体   English   中英

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

[英]matplotlib two different colors in the same annotate

我正在尝试在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'))

可能吗?

我不认为你可以在一个注释中有多种颜色,因为 - 据我所知 - 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. 我在位置fig1处创建了两个文本对象(这意味着图1的中心);
  3. 我手动改变了位置,直到它们与annotate生成的黑色文本大致重叠(结果是你在两次text调用中看到的值);
  4. 我设置注释color='white' ,因此它不会干扰重叠文本的颜色。

这是输出:

多色带注释的图形

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

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

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

在此输入图像描述

图像由以下代码生成。 使用matplotlib v2.1.2使用默认的matplotlibrc设置进行测试。

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. pgf.preamble package color
  3. 与第1和第2注释有一些重叠,因此更改了xytext
  4. 第二个注释中的color='g'实际上没有使用像rgb的(0,255,0)那样的纯“绿”颜色。 \\textcolor[rgb]{0.0, 0.5, 0.0}使它看起来很相似。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM