Python matplotlib 同一个注释中的两种不同颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24108063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
matplotlib two different colors in the same annotate
提问by user3720002
I am trying to create a figure in python and make is so that the same annonate text will have two colors, half of the annonate will be blue and the other half will be red.
我正在尝试在 python 中创建一个图形并且 make 是这样相同的注释文本将有两种颜色,注释的一半是蓝色,另一半是红色。
I think the code explain itself. I have 3 lines 1 green with green annonate, 1 blue with blue annonate.
我认为代码可以解释自己。我有 3 行 1 条绿色带绿色注释,1 条蓝色带蓝色注释。
The 3rd is red its the summation of plot 1 and plot 2, and I want it to have half annonate blue and half green.
第三个是红色,它是情节 1 和情节 2 的总和,我希望它有一半的蓝色和一半的绿色。
ipython -pylab
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'))
Is it possible?
是否可以?
采纳答案by gepcel
You can use r'$\textcolor{blue}{e^{-x/5}} + \textcolor{green}{e^{-x/1}}$'
to make the text half blue, half green. Using your own code for example:
您可以使用r'$\textcolor{blue}{e^{-x/5}} + \textcolor{green}{e^{-x/1}}$'
使文本一半蓝色一半绿色。例如使用您自己的代码:
The image is generated by the following code. Testd with matplotlib v2.1.2 with the default matplotlibrc
settings.
图像由以下代码生成。使用默认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')
It is mainly your code with the following changes:
主要是您的代码进行了以下更改:
- You need to use a
pgf
backend. - Usepackage
color
inpgf.preamble
- There's some overlapping with the 1st and 2nd annotations, so
xytext
is changed. - The
color='g'
in te 2nd annotation actually didn't use the pure "Green" color like (0, 255, 0) of rgb.\textcolor[rgb]{0.0, 0.5, 0.0}
makes it looking alike.
- 您需要使用
pgf
后端。 - 使用包
color
中pgf.preamble
- 与第一个和第二个注释有一些重叠,因此
xytext
已更改。 - 的
color='g'
在TE第二注释实际上并没有使用象RGB(0,255,0)的纯的“绿”的颜色。\textcolor[rgb]{0.0, 0.5, 0.0}
使它看起来很像。
回答by kadu
I don't think you can have multiple colours in a single annotation, since—as far as I know—annotate
only takes one text object as parameter, and text objects only support single colours. So, to my knowledge, there's no "native" or "elegant" way of automatically doing this.
我不认为在一个注释中可以有多种颜色,因为——据我所知——annotate
只接受一个文本对象作为参数,而文本对象只支持单一颜色。因此,据我所知,没有自动执行此操作的“本机”或“优雅”方式。
There is, however, a workaround: you can have multiple text objects placed arbitrarily in the graph. So here's how I'd do it:
但是,有一个解决方法:您可以在图表中任意放置多个文本对象。所以这就是我的方法:
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")
What I did was:
我所做的是:
- I set the annotation
color='black'
; - I created the two text objects at positions 0.5, 0.5 (which means the center of
fig1
); - I manually changed the positions until they were roughly overlapping with the black text generated by
annotate
(which ended up being the values you see in the two calls totext
); - I set the annotation
color='white'
, so it doesn't interfere with the colour of the overlapping text.
- 我设置了注释
color='black'
; - 我在位置 0.5、0.5(这意味着 的中心
fig1
)创建了两个文本对象; - 我手动更改了位置,直到它们与由 生成的黑色文本大致重叠
annotate
(最终是您在两次调用中看到的值text
); - 我设置了 annotation
color='white'
,所以它不会干扰重叠文本的颜色。
Here's the output:
这是输出:
It's not very elegant, and it does require some plotting to fine-tune the positions, but it gets the job done.
它不是很优雅,它确实需要一些绘图来微调位置,但它完成了工作。
If you need several plots, perhaps there's a way to automate the placement: If you don't store the fig1
object, the coordinates for text
become the actual x,y coordinates in the graph—I find that a bit harder to work with, but maybe it'd enable you to generate them automatically using the annotation's xy
coordinates? Doesn't sound worth the trouble for me, but if you make it happen, I'd like to see the code.
如果您需要多个绘图,也许有一种方法可以自动放置:如果您不存储fig1
对象,则 的坐标将text
成为图表中的实际 x、y 坐标——我发现使用起来有点困难,但也许它能让您使用注释的xy
坐标自动生成它们吗?对我来说听起来不值得麻烦,但如果你做到了,我想看看代码。