Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib two different colors in the same annotate

Tags:

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?

like image 252
user3720002 Avatar asked Jun 08 '14 16:06

user3720002


People also ask

How do you change the color of a marker in Pyplot?

You do not need to use format strings, which are just abbreviations. All of the line properties can be controlled by keyword arguments. For example, you can set the color, marker, linestyle, and markercolor with: plot(x, y, color='green', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=12).

How do I specify colors on a MatPlotLib plot?

The usual way to set the line color in matplotlib is to specify it in the plot command. This can either be done by a string after the data, e.g. "r-" for a red line, or by explicitely stating the color argument.

How do I annotate figures in MatPlotLib?

The annotate() function in pyplot module of matplotlib library is used to annotate the point xy with text s. Parameters: This method accept the following parameters that are described below: s: This parameter is the text of the annotation. xy: This parameter is the point (x, y) to annotate.


1 Answers

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.
like image 74
gepcel Avatar answered Sep 19 '22 06:09

gepcel