I have two float variables, let's say they are phi = 1.34e8 and beta = -2.7e-6. How do I display both results in a plot label in latex scientific notation over two lines? I want the plot label to look like (in latex font):
\phi = 1.34 x 10^8
\beta = -2.7 x 10^-6
And what about I have other variables for error, e.g. phi_err = 7.1e7, and I want the legend to look like:
\phi = (1.34 +/- 0.71) x 10^8
Edit:
My current Matlab code:
txt1 = texlabel(['n2=',num2str(n2)]);
txt2 = texlabel(['beta=',num2str(beta)]);
figure(1)
plot(...)
text(0.7,0.8,{txt1,txt2},'Units','normalized')
And the plot text looks like the upper part of the attached figure. How do I display the text in scientific notation with the multiply sign and the base 10 instead of e? Also, if I want to add the error (let's say I set in Matlab beta=[-2.7e-6, 1.2e-6] where beta(1) is the value and beta(2) is the error), then show should I modify the above code so that the result looks like the lower part of the attached figure? For the example I give, how do I extract 2.7 and 1.2 before the e? And what if they are of different order of magnitude, e.g. the error is 1.2e-7, which means in the displayed text I have to change it from 1.2e-7 to 0.12e-6 and combine the error and the beta value.
Related
Below is a minimal working problem, of what I am working on.
The file is a standard LaTeX file using sympy within pythontex, where I want to change
how sympy displays fractions.
Concretely I would like to make the following changes, but have been struggling:
How can I make sympy display the full, and not inline version of it's fractions for some of its fractions? In particular I would like the last fraction 1/5 to instead be displayed in full.
eg. \fraction{1}{5}
In the expression for the derivative, I have simplified the results, but I struggle to substitute the variable x with the fraction a/b. Whenever I substitute this expression into the fraction it fully simplifies the expression, which is not what I want. I just want to replace x with the fraction a/b (in this case 2/3 or 1/3 depending on the seed).
Below I have attached two images displaying what my code produces, and what I would like it to display. Do note that this is also stated in the two bullets above
Current output
Desired output
Code
\documentclass{article}
\usepackage{pythontex}
\usepackage{mathtools,amssymb}
\usepackage{amsmath}
\usepackage{enumitem}
\begin{document}
\begin{pycode}
import math
from sympy import *
from random import randint, seed
seed(2021)
\end{pycode}
\paragraph{Oppgave 3}
\begin{pycode}
a, b = randint(1,2), 3
ab = Rational(a,b)
pressure_num = lambda x: 1-x
pressure_denom = lambda x: 1+x
def pressure(x):
return (1-x)/(1+x)
pressure_ab = Rational(pressure_num(ab),pressure_denom(ab))
x, y, z = symbols('x y z')
pressure_derivative = simplify(diff(pressure(x), x))
pressure_derivative_ab = pressure_derivative.xreplace({ x : Rational(a,b)})
\end{pycode}
The partial pressure of some reaction is given as
%
\begin{pycode}
print(r"\begin{align*}")
print(r"\rho(\zeta)")
print(r"=")
print(latex(pressure(Symbol('\zeta'))))
print(r"\qquad \text{for} \ 0 \leq \zeta \leq 1.")
print(r"\end{align*}")
\end{pycode}
%
\begin{enumerate}[label=\alph*)]
\item Evaluate $\rho(\py{a}/\py{b})$. Give a physical interpretation of your
answer.
\begin{equation*}
\rho(\py{a}/\py{b})
= \frac{1-(\py{ab})}{1+\py{ab}}
= \frac{\py{pressure_num(ab)}}{\py{pressure_denom(ab)}}
\cdot \frac{\py{b}}{\py{b}}
= \py{pressure_ab}
\end{equation*}
\end{enumerate}
The derivative is given as
%
\begin{pycode}
print(r"\begin{align*}")
print(r"\rho'({})".format(ab))
print(r"=")
print(latex(pressure_derivative))
print(r"=")
print(latex(simplify(pressure_derivative_ab)))
print(r"\end{align*}")
\end{pycode}
\end{document}
Whenever I substitute this expression into the fraction it fully simplifies the expression, which is not what I want. I just want to replace x with the fraction a/b (in this case 2/3 or 1/3 depending on the seed).
It's possible to do this, if we use a with expression to temporarily disable evaluation for that code block, and then we use two dummy variables in order to represent the fraction, and finally we do the substitution with numerical values.
So the following line in your code:
pressure_derivative_ab = pressure_derivative.xreplace({ x : Rational(a,b)})
can be changed to:
with evaluate(False):
a1,b1=Dummy('a'),Dummy('b')
pressure_derivative_ab = pressure_derivative.subs(x,a1/b1).subs({a1: a,b1: b})
The expressions pressure_derivative and pressure_derivative_ab after this are:
How can I make sympy display the full, and not inline version of it's fractions for some of its fractions? In particular I would like the last fraction 1/5 to instead be displayed in full. eg. \fraction{1}{5}
For this, you only need to change this line:
= \py{pressure_ab}
into this line:
= \py{latex(pressure_ab)}
Because we want pythontex to use the sympy latex printer, instead of the ascii printer.
To summarize, the changes between the original code and the modified code can be viewed here.
All the code in this post is also available in this repo.
I want to plot only data points. Now I can plot the points which only considers 1 type of point. But my data contains different column variables. I want to plot different figures with different x and y variables from the data. Suppose I want to plot variable D against variable A or variable E against variable year but I want to plot data points with different colors or different types of points either *, dot, diamond etc. based on suppose, variable pub or variable E. Now for colormap I want to show colormap beside the figure with where the range of the variable value will be shown. For different type of points the point indexes will be suppose another variable E.
Also the 1st data should have a completely different point so that it can be distinguishable. My code actually shows different point for that data but it also plots with others.
Here is the truncated data.
Can anyone help me with that?
My code:
T = readtable('Data.xlsx');
year = T.Year;
pub = T.Publication;
A = T.A;
B = T.B;
C = T.C;
D = T.D;
E = T.F;
% Plot Data
f = figure;
%hold on; grid on, box on;
plot(A, D,'*')
hold on;
plot(A(1), D(1),'d')
It feels like this matlab example should be pretty close to what you want. It is a scatter plot (like your plot(A,D,'*') command), and has a colour scale that varies with a third variable c.
You should then combine this with a hold on command and plotting the first point using a different style suitable to your liking. You could something along the lines of the following (I have not downloaded your data, so I will use the example from the matlab link I provided):
x = linspace(0,3*pi,200); % Independent variable
y = cos(x) + rand(1,200); % Dependent variable
c = linspace(1,10,length(x)); % Colour variable
% Plot all points except the first one using colours in c and size 50:
scatter( x(2:end), y(2:end), 50, c(2:end) );
hold on
% Plot first point differently: twice the size, and with a filled marker:
scatter( x(1), y(1), 100, c(1), 'filled');
legend({'Data','First point'});
hold off
So I'm trying to plot three parametric functions using the gnuplot;
unfortunately, I cannot get around some garbage that is generated in the output plot. I tried to isolate the problem by splitting a function j into j1 and j2, just changing the position of the minus sign. Unexpectedly, the functions j1 and j2 jump strangely when close to the origin. I currently use version 4.6 of gnuplot, any suggestions?
CODE BELOW:
set parametric
j1(x) = -((1.0/27.0*(1.+9.*x))/2.0)**(1./3.) #negative portion
j2(x) = (-(1.0/27.0*(1.+9.*x))/2.0)**(1./3.)
k(x) = ((-x/3.0)**(3./2.))**(1./3.)
l(x) = -((-x/3.0)**(3./2.))**(1.0/3.0)
tt(x) = sqrt(-x/3.)
set trange [-1.0/3.0:0]
set yrange [0:1.0/3.0]
set xrange [-1./6.:1./3]
plot j1(t),tt(t) w l ls 1, j2(t),tt(t) w l ls 1, k(t),tt(t) w l ls 2, l(t),tt(t) w l ls 3
The problem comes from selecting the cube root of a negative number. Gnuplot can work with complex numbers, and in the complex number system there are three cube roots of any number†. For a real number, one of these is real and two are complex. Gnuplot is selecting the first‡ one which is complex for a negative number (for a positive number, the first one is real).
print (-8)**(1/3.0) # prints {1.0, 1.73205080756888}
The solution is to construct our own cube root function
cuberoot(x) = sgn(x)*abs(x)**(1/3.0)
This will select take the cube root of the absolute value (always positive) and make the result have the same sign as the original.
We can then use it in our functions
j1(x) = -cuberoot((1.0/27.0*(1.+9.*x))/2.0) #negative portion
j2(x) = cuberoot(-(1.0/27.0*(1.+9.*x))/2.0)
leaving the rest of the code alone.
Without custom cuberoot function
With custom cuberoot function
† For the given example of -8, they are 1 + 1.7320508i, -2, and 1 - 1.7320508i.
‡ When ordered in increasing order by complex argument (restricted to the interval [0,2π) ).
I try to plot the orbital velocity with gnuplot, but somehow gnuplot gets completely different results than me. Now from experience I think my values are correct but I checked it with Google's calculator and get my results.
I use the formula from Wikipedia and Google gets a velocity at apoapsis of about 2.2 km/s. Now gnuplot itself gets a velocity of about 3.2 km/s.
set xlabel "Altitude above sea level (meters)"
set ylabel "Orbital velocity (meters per second)"
set title "Velocity of an 80×100 km orbit around Kebrin"
set terminal png size 800,640
set output "orbitv.png"
set xrange [80000:100000]
G=6.674*10**-11
M=5.2915793*10**22
R=600000
plot sqrt(G*M*(2/(x+R)-1/(90000+R))) title 'Orbital velocity' with lines
I'm wondering were did I make the mistake? I copied the formula directly to Google and replaced G, M and R with the constant values and x with 100000 and get the result linked above.
This problem has to do with how gnuplot handles integers when doing arithmetic. When you have an expression like 1/(90000 + R), if R is an integer, gnuplot evaluates 1/(690000) = 0, which is perfectly valid integer arithmetic. The solution is to add a period to a number so that gnuplot knows to cast it as a floating-point number:
R = 600000. # short option
R = 600000.0 # clearer option
Another solution is to use e-notation for big numbers:
R = 6e5
Gnuplot treats that as a float. This also helps prevent order-of-magnitude/number-of-zeroes errors.
Incidentally, python and other languages have the same problem with integer arithmetic--watch out!
See this article Enclosed, but not Encrypted.
I have some binary data. I want to perform the gnuplots shown in that article, but using my data.
For a three-dimensional phase-space plot, the sequence a, b, c, d, e,
f, etc. can be used as space coordinates (a-b, b-c, c-d), (b-c, c-d,
d-e), (c-d, d-e, e-f), etc. Patterns in the plot created reveal
recurring relations between subsequent sequences. In this phase plot,
50,000 16-bit random numbers would produce an unstructured cloud of
dots.
I want to do exactly the same kind of thing. I have a binary file (about 10 MB) and I'd like to run it through gnuplot to create the nice gnuplot graphs.
What do I type into gnuplot to make that happen?
Doing a Google search for "phase space plot" and gnuplot doesn't return much. I don't know if that's because the article is a translation from German. I don't think I've found relevant answers in stack exchange sites.
To plot the 3d phase space use the following script, which works like the running average example from the gnuplot page:
reset
back4 = back3 = back2 = back1 = 0
shift(x) = (back4 = back3, back3 = back2, back2 = back1, back1 = x)
samples(x) = $0 < 3 ? NaN : x
set ticslevel 0
# the labels are only for orientation when checking the test data
set xlabel 'xlabel'
set ylabel 'ylabel'
splot 'randomdata.dat' using (shift($1), samples(back4-back3)):(samples(back3-back2)):(samples(back2-back1))
Gnuplot must hold four data values, which are stored in back1 to back4. For every new value, the stored values are shifted with shift. samples takes care that the first three values are not used, but only stored (NaN creates an invalid data point).
To test it, use this file randomdata.dat:
21
15
10
6
3
1
0
This plots four data points at (6,5,4), (5,4,3), (4,3,2), and (3,2,1).
If you have a binary data file with e.g. 16bit numbers, use
splot 'binaryfile' binary format="%ushort" using (shift($1), samples(back4-back3)):(samples(back3-back2)):(samples(back2-back1))
If you need to change the datasize, invoke gnuplot and type show datafile binary datasizes to see which formats are supported.