LaTeX Fractions

Lets-Plot now renders LaTeX fractions written as \frac{...}{...} in plot text elements, such as titles, axis labels, legends, tooltips and geom_text() / geom_label().

Note: Nested fractions are not supported.

In [1]:
import numpy as np

from lets_plot import *
In [2]:
LetsPlot.setup_html()
In [3]:
# Define the functions
def f1(x):
    return np.sin(3*x) / (x * (np.cos(x) + 2))

def f2(x):
    return np.cos(x) / x

def f3(x):
    return np.exp(-x)
In [4]:
c = ['#FFB6C1', '#AAFFAA', '#ADD8E6']
(ggplot()
  + geom_vline(xintercept=0, size=0.4)
  + geom_hline(yintercept=0, size=0.4)
  + geom_function(fun=f1, xlim=[0.1, 12], color=c[0], size=2,
                  n=300, manual_key=layer_key(r'\( \frac{sin(3x)}{x (cos(x) + 2)} \)', size=3))
  + geom_function(fun=f2, xlim=[0.1, 12], color=c[1],
                  n=300, manual_key=layer_key(r'\( \frac{cos(x)}{x} \)', size=3))
  + geom_function(fun=f3, xlim=[0.1, 12], color=c[2],
                  n=300, manual_key=layer_key(r'\( e^{-x} \)', size=3))
  + scale_x_continuous(expand=[0, 0.2])
  + scale_y_continuous(limits=[-0.4, 1.0])
  + labs(
      x='x',
      y='f(x)',
      title='Mathematical Functions'
    )
  + theme_classic()

  + theme(
      # Titles, labels
      text=element_text(face='italic', family='Computer Modern'),
      plot_title=element_text(size=20, hjust=0.05),
      axis_title=element_text(size=18),
      legend_text=element_text(size=18, vjust=0.5),
      # Legend
      legend_background=element_rect(size=1),
      legend_position=[0.95, 0.9],
      legend_justification=[1, 1],
      # Axis, gridlines
      axis_line='blank', axis_ticks='blank',
      panel_grid_major=element_line(linetype='longdash'),
  )
  + theme(
      # Layout improvements
      panel_inset=4,
      plot_inset=0,
      axis_text=element_text(margin=0),
      axis_title=element_text(margin=0),
      plot_margin=16
  )

  + flavor_solarized_dark()
  + ggsize(900, 500)
)
Out[4]: