【问题标题】:matplotlib two different colors in the same annotatematplotlib 在同一个注解中有两种不同的颜色
【发布时间】:2014-06-08 16:07:46
【问题描述】:

我正在尝试在 python 中创建一个图形,并且使相同的 annonate 文本具有两种颜色,一半 annonate 将是蓝色,另一半将是红色。

我认为代码可以自我解释。我有 3 行,1 条绿色带绿色番石榴,1 条蓝色带蓝色番石榴。

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

有可能吗?

【问题讨论】:

标签: python matplotlib


【解决方案1】:

您可以使用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')

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

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

【讨论】:

  • 谢谢,这行得通。在我自己的情况下,我在使用 TeX 时遇到了记忆问题,这可能是由于要在散点图中绘制大量的点。我通过使用"lualatex" 解决了这个问题texsystem
  • @bli,是的,matplotlib.rc('pgf', texsystem='pdflatex') 的那一行应该被删除。我认为默认的 tex 系统也可以。
  • 在 Jupyter 中尝试此操作,我在 matplotlib.use('pgf') 线上收到警告 anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:14: UserWarning: matplotlib.pyplot as already been imported, this call will have no effect.。然后,当我尝试使用\textcolor 时,我收到错误ValueError: \textcolor{blue}{e^{-x/5}} + \textcolor{green}{e^{-x/1}} ^ Unknown symbol: \textcolor (at char 0), (line:1, col:1)。知道如何解决这个问题吗? `
  • 来自matplotlib doc, If you use the matplotlib.use() function, this must be done before importing matplotlib.pyplot. Calling use() after pyplot has been imported will have no effect.
  • @gepcel 我遇到了这个错误:ValueError: Error processing '\(\displaystyle \textcolor{blue}{e^{-x/5}} + \textcolor[rgb]{0.0, 0.5, 0.0}{e^{-x/1}}\)' LaTeX Output: ! Undefined control sequence. <argument> ...lectfont \(\displaystyle \textcolor {blue}{e^{-x/5}} + \textco... <*> ...\textcolor[rgb]{0.0, 0.5, 0.0}{e^{-x/1}}\)} ! ==> Fatal error occurred, no output PDF file produced! Transcript written on texput.log. 请帮助解决这个问题。提前致谢。
【解决方案2】:

我认为您不能在一个注释中使用多种颜色,因为据我所知,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',所以不会干扰重叠文字的颜色。

这是输出:

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

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

【讨论】:

  • 在绘制这个之后,我意识到你的情节中有一个+,我以前没有注意到。我相信这些说明足以让你用额外的text 绘制它。另请查看this answer,它提供了一种更优雅的方法,但我无法使其工作。
猜你喜欢
  • 2014-04-03
  • 2015-08-11
  • 2015-05-31
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多