matplotlib two different colors in the same annotate

后端 未结 2 1324
眼角桃花
眼角桃花 2020-12-24 08:15

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.

2条回答
  •  难免孤独
    2020-12-24 08:24

    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:

    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.

提交回复
热议问题