Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

LaTeX lets you create lovely, complex mathematical functions from typed text. Plotly will render LaTeX in annotations, labels, and titles. In this post, we’ll show how it works.

### 1. MATLAB plotting with LaTeX

First, here’s an example using Plotly’s MATLAB API. This is a visualization of Bessel Functions of the first kind, solutions for a differential equation. It’s a useful function for studying and understanding heat conduction, how waves travel, and more. The MATLAB code below creates our plot.

plot(X,J,'LineWidth',1.5)
axis([0 20 -.5 1])
grid on
legend('J_0','J_1','J_2','J_3','J_4','Location','Best')
title('Bessel Functions of the First Kind for v = 0,1,2,3,4')
xlabel('X')
ylabel('J_v(X)')
X = 0:0.1:20;
X = 0:0.1:20;
J = zeros(5,201);
for i = 0:4
J(i+1,:) = besselj(i,X);
end
plot(X,J,'LineWidth',1.5)
axis([0 20 -.5 1])
grid on
legend('J_0','J_1','J_2','J_3','J_4','Location','Best')
title('Bessel Functions of the First Kind for v = 0,1,2,3,4')
xlabel('X')
ylabel('J_v(X)')
fig2plotly();


The code generates a web-based version of our plot. We can apply a theme to change the colors, layouts, and fonts.

Then, we can share the plot in an iframe, as seen below.

The x axis contains the following formula. Plotly renders the LaTeX version of it.

$x^2 frac{d^2 y}{dx^2} + x frac{dy}{dx} + (x^2 – alpha^2)y = 0$

The plot is saved at a URL: https://plot.ly/~MattSundquist/2135. The URL contains the data, plot and code to translate the plot between MATLAB, R, Python, Julia, and JavaScript.

### 2. Python and matplotlib plotting with LaTeX

We can make matplotlib and Python plots into web-based plots. This is an example using Plotly’s Python API. Here we’re using a Gaussian distribution to study random variables and see where they fall on what is sometimes called a “bell curve.” We can add the standard deviation formula to our plot.

import matplotlib.pyplot as plt   # side-stepping mpl's backend
import plotly.plotly as py
import plotly.tools as tls
from plotly.graph_objs import *
%matplotlib inline
py.sign_in("IPython.Demo", "1fw3zw2o13")
fig1 = plt.figure()

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-2.0, 2.0, 10000) # The x-values
sigma = np.linspace(0.4, 1.0, 4) # Some different values of sigma

# Here we evaluate a Gaussians for each sigma
gaussians = [(2*np.pi*s**2)**-0.5 * np.exp(-0.5*x**2/s**2) for s in sigma]

ax = plt.axes()

for s,y in zip(sigma, gaussians):
ax.plot(x, y, lw=1.25, label=r"$sigma = %3.2f$"%s)

formula = r"$y(x)=frac{1}{sqrt{2pisigma^2}}e^{-frac{x^2}{2sigma^2}}$"

ax.text(0.05, 0.80, formula, transform=ax.transAxes, fontsize=20)
ax.set_xlabel(r"$x$", fontsize=18)
ax.set_ylabel(r"$y(x)$", fontsize=18)
ax.legend()
plt.show()


Here is our plot:

The annotation looks like this in the GUI:

### 3. R plotting with LaTeX

We can make plots with R. Here’s an example using the Plotly R API.

library(plotly)

trace1 <- list(
x = c(1, 2, 3, 4),
y = c(1, 4, 9, 16),
name = "$alpha_{1c} = 352 pm 11 text{ km s}^{-1}$",
type = "scatter"
)
trace2 <- list(
x = c(1, 2, 3, 4),
y = c(0.5, 2, 4.5, 8),
name = "$beta_{1c} = 25 pm 11 text{ km s}^{-1}$",
type = "scatter"
)
data <- list(trace1, trace2)
layout <- list(
xaxis = list(title = "$sqrt{(n_text{c}(t|{T_text{early}}))}$"),
yaxis = list(title = "$d, r text{ (solar radius)}$")
)
response <- py$plotly(data, kwargs=list(layout=layout, filename="latex", fileopt="overwrite")) url <- response$url

The title was added in the GUI, and is written as ‘$LaTeX$’. We embed with this snippet; every Plotly graph can similarly be embedded in websites, blogs, and notebooks.

<iframe width="450" frameborder="0" seamless="seamless" scrolling="no" src="https://plot.ly/~MattSundquist/2138.embed?width=500&height=500"></iframe>


### 4. Mathematica plotting with LaTeX

A user-contributed Mathematica API is in the works, which lets us turn our Mathematica plots into D3, web-based plots. Here is our code:

Plotly[Sin[Exp[x]], {x, -Pi, Pi}, AxesLabel -> {"e", "s"}]


And our plot:

Plotly is free for public projects, entirely online, and you own your data. Learn more on our site.