I have a function of two parameters f(x,s). In Wolfram Mathematica I use special function DensityPlot[f, {x, 0, 2}, {s, -150, 150}, PlotPoints -> 200] and I get some kind of density projection to x-s plain. How can I do the same task in Latex?
Thanks!

- 29,535
- 11
-
2Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 4.0 license). By SE policy, any vandalism will be reverted. – tripleee Apr 26 '21 at 08:16
-
It is presumably because of the -1. The person giving it should have left a comment as to why they gave a downvote. – daleif Apr 26 '21 at 08:43
-
1Not my downvote, but the absence of any demonstration of effort could easily attract some downvotes. Separately reminding every new user to please review the site guidelines for every downvote is not sustainable. – tripleee Apr 26 '21 at 08:47
-
Look into the sagetex package. It gives you access to the Sage CAS. The documentation here indicates "The Mathematica interface will only work if Mathematica is installed on your computer with a command line interface that runs when you give the math command. The interface lets you send certain Sage objects to Mathematica, run Mathematica functions, import certain Mathematica expressions to Sage, or any combination of the above.". I don't have Mathematica so can't check. – DJP Apr 27 '21 at 01:18
-
Does this answer your question? Create heatmap with PSTricks or TikZ – Elad Den Apr 27 '21 at 06:31
2 Answers
I don't know the DensityPlot function, but because LaTeX is no mathematical tool, there is a point when a certain complexity exists that the computation should be done with another tool. Then you can decide to
- either export the data to a file which can be read by LaTeX again to then only plot the data
- or to export the resulting plot (without any axis and labels) with can then be used to "surround" it with axes and labels in LaTeX style.
One of the packages to draw plots in LaTeX is pgfplots and I will only talk about that package in the rest of this answer.
To do 1. you can use the \addplot table command and you will find plenty of examples here on TeX.SX showing you how this works. To do 2. you can use the \addplot graphics command which I will show in the below code. This is quite seldom asked for or provided as an answer (to my opinion) so I thought this is an opportunity to show it again, since you provided already an image which could be cropped to fulfill the above stated requirements.
% used PGFPlots v1.17
% (based on <https://tex.stackexchange.com/a/397669/95441>)
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot graphics [
xmin=0,
xmax=2,
ymin=-150,
ymax=150,
% cropped plot from question
] {DensityPlot};
\end{axis}
\end{tikzpicture}
\end{document}
As you can see this is the bare minimum you need to do, but you are free to change everything you like, e.g. the size of the plot, the axis limits, the axis ticks, the axis labels. Also for that you will find plenty of examples here on TeX.SX and of course in the PGFPlots manual which also has a lot of given examples. Just scroll through the manual and have a look at the examples. If you find something of interest have a look at the given code and I am pretty sure the rest you can do on your own ;)
Just if you are curious about the cropped image used in the above code: It is (scaled down to a width of 250)
- 29,535
I'm sure this can be done purely using LaTeX and packages but you can also save a mathematica file, normally .nb, as a .tex file which will automatically convert to LaTeX and save an image which can then be included.
Of course you could also just save the image and then include this in LaTeX with `\includegraphics'.
.nb
f[x_, s_] := 1/(x^2 + s^2)
DensityPlot[f[x, s], {x, 0, 2}, {s, 0, 2}, PlotPoints -> 200]
.tex (note I just saved it as unititled-2.tex)
%% AMS-LaTeX Created with the Wolfram Language for Students - Personal Use Only : www.wolfram.com
\documentclass{article}
\usepackage{amsmath, amssymb, graphics, setspace}
\newcommand{\mathsym}[1]{{}}
\newcommand{\unicode}[1]{{}}
\newcounter{mathematicapage}
\begin{document}
\begin{doublespace}
\noindent(\pmb{f[\text{x$_$}, \text{s$_$}]\text{:=}1\left/\left(x^2+s^2\right)\right.})
\end{doublespace}
\begin{doublespace}
\noindent(\pmb{\text{DensityPlot}[f[x, s], {x, 0, 2}, {s, 0, 2}, \text{PlotPoints}\to 200]})
\end{doublespace}
\includegraphics{Untitled-2_gr1.eps}
\end{document}
.pdf after running LaTeX:
- 3,649

