楼主: janyiyi
1316 0

Four Beautiful Python, R, MATLAB, and Mathematica plots with LaTeX [推广有奖]

  • 3关注
  • 17粉丝

讲师

27%

还不是VIP/贵宾

-

威望
0
论坛币
3206 个
通用积分
5056.6800
学术水平
539 点
热心指数
537 点
信用等级
538 点
经验
10157 点
帖子
300
精华
2
在线时间
90 小时
注册时间
2010-10-3
最后登录
2024-4-6

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
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.


  1. plot(X,J,'LineWidth',1.5)
  2. axis([0 20 -.5 1])
  3. grid on
  4. legend('J_0','J_1','J_2','J_3','J_4','Location','Best')
  5. title('Bessel Functions of the First Kind for v = 0,1,2,3,4')
  6. xlabel('X')
  7. ylabel('J_v(X)')
  8. X = 0:0.1:20;
  9. X = 0:0.1:20;
  10. J = zeros(5,201);
  11. for i = 0:4
  12.     J(i+1,:) = besselj(i,X);
  13. end
  14. plot(X,J,'LineWidth',1.5)
  15. axis([0 20 -.5 1])
  16. grid on
  17. legend('J_0','J_1','J_2','J_3','J_4','Location','Best')
  18. title('Bessel Functions of the First Kind for v = 0,1,2,3,4')
  19. xlabel('X')
  20. ylabel('J_v(X)')
  21. 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.

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

  8. import matplotlib.pyplot as plt
  9. import numpy as np

  10. x = np.linspace(-2.0, 2.0, 10000) # The x-values
  11. sigma = np.linspace(0.4, 1.0, 4) # Some different values of sigma
复制代码
  1. # Here we evaluate a Gaussians for each sigma
  2. gaussians = [(2*np.pi*s**2)**-0.5 * np.exp(-0.5*x**2/s**2) for s in sigma]

  3. ax = plt.axes()

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

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

  7. ax.text(0.05, 0.80, formula, transform=ax.transAxes, fontsize=20)
  8. ax.set_xlabel(r"$x$", fontsize=18)
  9. ax.set_ylabel(r"$y(x)$", fontsize=18)
  10. ax.legend()
  11. 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.

  1. library(plotly)
  2. py <- plotly(username="R-demo-account", key="yu680v5eii")

  3. trace1 <- list(
  4.   x = c(1, 2, 3, 4),
  5.   y = c(1, 4, 9, 16),
  6.   name = "$alpha_{1c} = 352 pm 11 text{ km s}^{-1}$",
  7.   type = "scatter"
  8. )
  9. trace2 <- list(
  10.   x = c(1, 2, 3, 4),
  11.   y = c(0.5, 2, 4.5, 8),
  12.   name = "$beta_{1c} = 25 pm 11 text{ km s}^{-1}$",
  13.   type = "scatter"
  14. )
  15. data <- list(trace1, trace2)
  16. layout <- list(
  17.   xaxis = list(title = "$sqrt{(n_text{c}(t|{T_text{early}}))}$"),
  18.   yaxis = list(title = "$d, r text{ (solar radius)}$")
  19. )
  20. response <- py$plotly(data, kwargs=list(layout=layout, filename="latex", fileopt="overwrite"))
  21. 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="500" height="500" 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:

  1. 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.


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:mathematica Mathematic beautiful Thematic MATLAB function equation studying creates example

已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
oliyiyi + 20 + 20 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 20  论坛币 + 20  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-26 15:42