matplotlib两个不同的颜色在同一个环中

2024-09-25 02:37:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python创建一个图形,make is使同一个annonate文本有两种颜色,一半是蓝色,另一半是红色。

我认为代码可以解释自己。我有三条线,一条绿色和绿色的annonate,一条蓝色和蓝色的annonate。

第三个是红色的,它是1号和2号地块的总和,我希望它有一半是安妮蓝一半是绿色。

伊普顿-派拉布

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'))

有可能吗?


Tags: plotdictpointsoffsetcolor蓝色xy绿色
2条回答

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

enter image description here

图像由以下代码生成。使用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中使用包color
  3. 与第一个和第二个注释有一些重叠,因此xytext被更改。
  4. te 2nd注释中的color='g'实际上没有使用像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")

我所做的是:

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

输出如下:

multi-colour annotated graph

它不是很优雅,它确实需要一些阴谋来微调位置,但它完成了工作。

如果您需要几个绘图,也许有一种方法可以自动放置:如果您不存储fig1对象,那么text的坐标将成为图形中的实际x,y坐标-我发现这有点难处理,但也许它可以使您使用注释的xy坐标自动生成它们?听起来不值得我费心,但如果你能做到,我想看看代码。

相关问题 更多 >