pythonmatplotlib

matplotlib two different colors in the same annotate


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.

I think the code explain itself. I have 3 lines 1 green with green annonate, 1 blue with blue annonate.

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.

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?


Solution

  • 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:

    enter image description here

    The image is generated by the following code. Testd with matplotlib v2.1.2 with the default matplotlibrc settings.

    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:

    1. You need to use a pgf backend.
    2. Usepackage color in pgf.preamble
    3. There's some overlapping with the 1st and 2nd annotations, so xytext is changed.
    4. 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.