ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 25 Nov 2021 10:38:33 +0100LaTeX can't find file for text in plothttps://ask.sagemath.org/question/59948/latex-cant-find-file-for-text-in-plot/Apologies in advance if I've missed something obvious. I'm trying to add LaTeX text to a plot, e.g.
from matplotlib import rc
rc('text', usetex=True)
plot(x^2, legend_label=r'This is $x^2$')
and each time, I get an error message like this:
/opt/sagemath-9.3/local/lib/python3.7/site-packages/sage/repl/rich_output/display_manager.py:596: RichReprWarning: Exception in _rich_repr_ while displaying object: latex was not able to process the following string:
b'lp'
Here is the full report generated by latex:
This is pdfTeX, Version 3.141592653-2.6-1.40.23 (MiKTeX 21.10)
entering extended mode
! I can't find file `/home/sage/.sage/matplotlib-1.5.1/tex.cache/1acea6f6c115d0
ec7a634ed0529287b9.tex'.
<*> ....cache/1acea6f6c115d0ec7a634ed0529287b9.tex
Please type another input file name
! Emergency stop.
<*> ....cache/1acea6f6c115d0ec7a634ed0529287b9.tex
No pages of output.
Transcript written on texput.log.
RichReprWarning,
The file in question (1acea6f6c115d0ec7a634ed0529287b9.tex) does exist, but the path is
`/home/.sage/matplotlib-1.5.1/tex.cache/1acea6f6c115d0
ec7a634ed0529287b9.tex'
not
`/home/sage/.sage/matplotlib-1.5.1/tex.cache/1acea6f6c115d0
ec7a634ed0529287b9.tex'
For whatever it's worth, my home directory is `C:\Users\davis\` and if I copy the relevant file into the folder that LaTeX says it's looking for, and then run the same code, I get the same error message.
I'm on Windows 10 running Sage 9.3 (fresh installation this morning).xdavThu, 25 Nov 2021 10:38:33 +0100https://ask.sagemath.org/question/59948/ymin and ymax not workinghttps://ask.sagemath.org/question/59625/ymin-and-ymax-not-working/Running this code
f_x = (x**3 - x**2 + 3*x + 4) / (x**2 - 4*x + 3)
plot(f_x, (x, 0, 4), detect_poles=True, ymin=-100, ymax=100)
results in a graph with a max y of about 500000.
What am I doing wrong? The x-axis is limited, as I want, in the range 0 to 4.SageSun, 07 Nov 2021 14:26:58 +0100https://ask.sagemath.org/question/59625/ymin and ymax not workinghttps://ask.sagemath.org/question/59626/ymin-and-ymax-not-working/ When I run this code:
f_x = (x**3 - x**2 + 3*x + 4) / (x**2 - 4*x + 3)
plot(f_x, x+3, (x, 0, 4), detect_poles=True, ymin=-100, ymax=100)
the result is a graph with ymax ~ 500000 and ymin ~(- 1000). What am I doing wrong?SageSun, 07 Nov 2021 14:29:01 +0100https://ask.sagemath.org/question/59626/Why is Sage calling Wine to show 2D plots and how do I fix it?https://ask.sagemath.org/question/59536/why-is-sage-calling-wine-to-show-2d-plots-and-how-do-i-fix-it/This is a two-parter. Part the first is mainly out of curiosity. Part the second carries more weight.
I'm running an Arch (Manjaro) Linux machine. I have a fresh install of `sagemath` via `pacman`. My system is up to date, and I can open the `sage` console by calling it from the command line. I'd like to plot a 2D graph of a Brownian motion algorithm, but whenever I call it, Wine opens. The first time my graph was compiled, a dialog box opened and it said something along the lines of "Do you want to run this using Wine?". I clicked yes. It no longer shows up. I don't have the exact line. I uninstalled and reinstalled Sage and deleted the cache. I wasn't able to replicate the dialog box. Anyway.
Whenever I run `plot(<func>)`, the prompt reads back that it is launching a png viewer for a Graphics object, which I understand to be the correct output. But again, it opens in Wine via a Windows 95 looking Explorer window. Here's the path.
`file:Z:\home\def\.sage\temp\tower\17759\tmp_esqczsyp.png`
Why is Wine the default program to open a 2D plot? Curiously, I can show 3D plots just fine.dfarmi2Fri, 29 Oct 2021 19:37:09 +0200https://ask.sagemath.org/question/59536/Am I doing something wrong in plot or is this a bug?https://ask.sagemath.org/question/58967/am-i-doing-something-wrong-in-plot-or-is-this-a-bug/I put this into sagecell online and get a messed up plot. f(x) simplifies to 1.
f(x)=2/3*(cos(x)^2 + cos(x-(2/3)*pi)^2 + cos(x-(4/3)*pi)^2)
(plot( f , -4,4))
John Paul MorrisonSat, 11 Sep 2021 22:32:08 +0200https://ask.sagemath.org/question/58967/Plot with no scale on the y axishttps://ask.sagemath.org/question/57439/plot-with-no-scale-on-the-y-axis/ If I plot a function that generates huge numbers, then sage normalizes it between -1 and 1. I don't like this behaviour because it hides the real values on the y scale.
For example, if I do
plot(10^6 * x, -1, 1)
what I get is a plot between -1 and 1 on both the axis. Is there a way to have the values on the y axis with the right scale?stepFri, 04 Jun 2021 02:21:11 +0200https://ask.sagemath.org/question/57439/Plot points of different sizeshttps://ask.sagemath.org/question/57173/plot-points-of-different-sizes/Say I have a map M:
[0,1] -> .3
[1,2] -> 1.1
[-3,-3] -> .6
etc.
I want to make a scatter plot where the point at P has size M[P]. How do I do this? Presumably I use the point2d object, which takes a size parameter.ant314159265Thu, 20 May 2021 00:32:57 +0200https://ask.sagemath.org/question/57173/Adapt 2D-plot tick label attributes to dateshttps://ask.sagemath.org/question/51080/adapt-2d-plot-tick-label-attributes-to-dates/Is there a way to access ticks labels to rotate them? I've tried `Graphics.matplotlib()` and `set_rotation()`, but this doesn't seem to produce changes. Am I doing wrong things?
In the example below, the formatter and locator are working correctly, but the problem is all labels are oriented horizontally, messing all together since each tick label is a date and therefore quite long. Need to rotate them.
import csv
from datetime import datetime
from matplotlib import ticker
from matplotlib import dates
data = [('04/22/20', '04/23/20', '04/24/20','04/25/20','04/26/20', '04/27/20'),
(20, 40, 80, 160, 320, 640)]
labels = data[0]
labels = map(lambda x: dates.date2num(datetime.strptime(x, '%m/%d/%y')), labels)
labels = list(labels)
values = data[1]
values = map(lambda x: int(x), values)
# Z is a list of [(x1, y1), (x2, y2)...]
# x1, x2, ... are dates
# y1, y2, ... are values
Z = zip(labels, values)
Z = list(Z)
p = list_plot(Z, ticks=[1, None],
tick_formatter=[dates.DateFormatter('%d.%m.%Y'), None],
axes_labels=['Days', '$\\log \\;{N}$'], plotjoined=True,
thickness=2, figsize=4, scale='semilogy')
G = p.matplotlib()
labels = G.axes[0].xaxis.get_ticklabels()
labels = list(labels)
for label in labels:
label.set_rotation(45)
p
This outputs the plot with an ugly x-axis on which all the dates are messed up. How to fix that?
![Plot with dates as tick labels on the x-axis](https://i.imgur.com/14VIFv4.png)MrDvbnhbqMon, 27 Apr 2020 20:59:05 +0200https://ask.sagemath.org/question/51080/Background for plot should include axes and labelshttps://ask.sagemath.org/question/56826/background-for-plot-should-include-axes-and-labels/## Plots in Jupyter: Mathematica vs Sage
Below are four versions of a same plot in Jupyter notebooks in JupyterLab:
- light theme vs dark theme
- Mathematica vs Sage, using the Wolfram Language kernel
and the SageMath kernel
To reproduce, either use the notebooks: [mathematica](https://cdn.discordapp.com/attachments/531944364554059778/836651999353241610/jupiter-gravity-gradient-mathematica.ipynb) | [sage](https://cdn.discordapp.com/attachments/531944364554059778/836652569568739388/jupiter-gravity-gradient-sage.ipynb), or use the following code snippet:
f = 8.252e-5 - 0.035e-10*x
I = (0, 1e5)
labels = ["altitude in meters", "force difference in newtons"]
plot(f, I, axes_labels=labels)
and in JupyterLab, select light or dark via the "Settings > JupyterLab Theme" menu.
### Light theme
Left: Mathematica. Right: SageMath.
![](https://cdn.discordapp.com/attachments/531944364554059778/836205750984507402/sage-light.png)
### Dark theme
Left: Mathematica. Right: SageMath.
![](https://cdn.discordapp.com/attachments/531944364554059778/836205951635292190/sage-dark.png)
### Observation
In the SageMath plots,
- the font size is inconsistent between the axis labels and tick labels
- the white background does not include the axes or tick labels
if the plot does not extend to an area covering them;
as a consequence, in dark mode,
- the y-axis label and y-axis tick labels,
- the x-axis, x-axis label and x-axis tick labels,
are black on a black background, and cannot be seen.
### Question
Could i make the background permanently opaque,
and permanently change the font size to something
more reasonable like Mathematica has for reference?erentarTue, 27 Apr 2021 13:45:35 +0200https://ask.sagemath.org/question/56826/Formatting plot titlehttps://ask.sagemath.org/question/56685/formatting-plot-title/ I am not able to find formatting options for `title` for `plot`. Here's the code,
p1 = plot(fresnel_sin(t), (t, -pi, pi), ticks=[pi/4,0.1], tick_formatter=[pi,None], legend_label=r'$f(x) = \int_0^x \sin(\frac{\pi t^2}{2})dt$', axes_labels=[r'$x$', r'$f(x)$'], axes_labels_size=1.5, title='Fresnel\'s sin', title_pos=(0.5,1.1))
p1.set_legend_options(back_color=(0.9,0.9,0.9), shadow=False, font_size=14, loc=(0,0.7))
p1.show()
The `title` font is relatively small, I am not able to change it without making it look displeasing. I've tried the following modification,
p1 = plot(fresnel_sin(t), (t, -pi, pi), ticks=[pi/4,0.1], tick_formatter=[pi,None], legend_label=r'$f(x) = \int_0^x \sin(\frac{\pi t^2}{2})dt$', axes_labels=[r'$x$', r'$f(x)$'], axes_labels_size=1.5)
p1.set_legend_options(back_color=(0.9,0.9,0.9), shadow=False, font_size=14, loc=(0,0.7))
p2 = text('Fresnel\'s sin ', (1.5, 0.9), fontsize=14, color='black')
p1 + p2
But it's not what I want. Using `text` doesn't seem to do it(or so I think). Need some help here :).debojit2178Sat, 17 Apr 2021 12:59:34 +0200https://ask.sagemath.org/question/56685/Possible problem in bezier_path.pyhttps://ask.sagemath.org/question/56608/possible-problem-in-bezier_pathpy/ It seems that the path that is stored by BezierPath is not correct.
Let
p2d = [[(3.0,0.0),(3.0,0.13),(2.94,0.25),(2.8,0.35)],
[(2.7,0.44),(2.6,0.5),(2.5,0.5)],
[(2.36,0.5),(2.24,0.44),(2.14,0.35)],
[(2.05,0.25),(2.0,0.13),(2.0,6.1e-17)]]
then
bezier_path(p2d)[0].path
is different than the given path p2d. There are several consequences to this, for example, the stored path cannot be fed again to bezier_path, so this won't work (results in weird errors):
bezier_path(bezier_path(p2d)[0].path)
and also affects the plot3d method of the class.
The issue seems to stem from line 64 of bezier_path.py:
vertices = self.path[0]
Since the list of vertices changes size after the line so does self.path. I believe the line should be
vertices = deepcopy(self.path[0])RozaThSun, 11 Apr 2021 02:07:59 +0200https://ask.sagemath.org/question/56608/csc plotting helphttps://ask.sagemath.org/question/56334/csc-plotting-help/Hello,
I'm new to SAGE and am having trouble plotting a cosecant (csc) curve.
Here is my input:
plot(csc(x), (x,-3*pi, 3 * pi))
but the output doesn't show the curves approaching the asymptotes as I would expect.
What do folks think about this?
Thanks for the help!
-mattmzagrabeTue, 23 Mar 2021 17:17:18 +0100https://ask.sagemath.org/question/56334/Pan and zoom in plot 2Dhttps://ask.sagemath.org/question/56106/pan-and-zoom-in-plot-2d/I'm new in sagemath, using plot3d I can zoom and pan in graphic but I can't do it in 2d. Is there a way to have same features?dkWed, 10 Mar 2021 17:03:26 +0100https://ask.sagemath.org/question/56106/kernel dies on plothttps://ask.sagemath.org/question/55774/kernel-dies-on-plot/I have a clean installation of SageMath on Windows 10:
SageMath version 9.2, Release Date: 2020-10-24. Using Python 3.7.7.
When I try to plot anything with SageMath notebook, e.g.
P = plot(sin, (0, 10))
P
the kernel dies. This happens on Firefox and Chrome.
How can this be? Many thanks.OldMortalityFri, 19 Feb 2021 21:28:01 +0100https://ask.sagemath.org/question/55774/How show vector or matrix in legend label of plot in latex?https://ask.sagemath.org/question/55304/how-show-vector-or-matrix-in-legend-label-of-plot-in-latex/We can insert latex in legend labels by surrounding the text with '$'. But it doesn't work for a vector (matrix, array), e.g.
\left[\begin{array}{r} 1 \\ 0 \end{array}\right]
Is there any way to display vector in latex in legend label?robSat, 16 Jan 2021 14:32:42 +0100https://ask.sagemath.org/question/55304/How plot methods?https://ask.sagemath.org/question/54598/how-plot-methods/I use
r = 1.1
r.eint().n(digits=10)
output 2.167378280
But
plot(x.eint(), x, [-1,1],frame=True)
causes error: AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'eint'
How can I plot also functions defined as methods?
AndrMon, 07 Dec 2020 13:04:50 +0100https://ask.sagemath.org/question/54598/Argand diagram plothttps://ask.sagemath.org/question/38587/argand-diagram-plot/HI
T=[-sqrt(1/2*sqrt(13) + 1) - I*sqrt(1/2*sqrt(13) - 1),
-sqrt(1/2*sqrt(13) + 1) + I*sqrt(1/2*sqrt(13) - 1),
-sqrt(1/2*sqrt(13) + 1) +I*sqrt(1 + sqrt(13)/2),
-sqrt(1/2*sqrt(13) + 1) -I*sqrt(1 + sqrt(13)/2) ]
what is the best , fastest, way to plot Argand diagram of T ?ortolljSun, 20 Aug 2017 11:40:07 +0200https://ask.sagemath.org/question/38587/Downloading thousands of plots from a server sage notebook?https://ask.sagemath.org/question/54495/downloading-thousands-of-plots-from-a-server-sage-notebook/Hello! I made a few thousand 2D plots on an online sage notebook (legacy notebook, not jupyter). They are stored in a list. The computation took over a week and I'd like to avoid having to rerun.
What's the best way to download all of these plots as PNG files?
In the past, I have hacked it by looping over the list and .show()'ing the plots, downloading the notebook's HTML file, and cutting the plots from the directory (don't laugh). Usually showing a few hundred plots takes a while to show and I'm worried it may run out of memory and crash in this case.
Thanks in advance!
Edit: I should also mention I am not an admin on the server.TheHiggsBozoTue, 01 Dec 2020 19:28:27 +0100https://ask.sagemath.org/question/54495/Some 2-d plot questionshttps://ask.sagemath.org/question/54462/some-2-d-plot-questions/1. How draw 2 or more functions on one image to compare?
2. On AxisY (if y is small) I see 1,2,3 but I don't know if 1 means 1e-6, 1e-8 or 1e-10. Is possible draw this information on image?
3. For very small y , is order of magnitude 1e-16, are visible rounding errors. Is possible draw with more precision than double? I see "fast_callable" example but this not works in my case.
Example for question 3.
Goal: plot `p(x)-exp(x)` where `p` is a Chebyshev approximation of `exp` of degree > 10.
For a concrete example, let us use the degree 11
Chebyshev approximation for the exponential.
We rescale the x-axis from [-1,1] to [0,1],
and thus change `exp(x)` to `exp(x/2 + 1/2)`.
We use this Chebyshev approximation.
f(x) = (1.648721270700127734695199 + 0.8243606353500639704213267*x + 0.2060901588375456897278408*x^2 + 0.03434835980625555348009372*x^3 + 0.004293544975435477755738488*x^4 + 0.0004293544975550920330337524*x^5 + 3.577954294118110098524956e-05*x^6 + 2.555681612268918733452545e-06*x^7 + 1.597272498430465439987321e-07*x^8 + 8.873762489265668731711247e-09*x^9 + 4.462222789173961848447732e-10*x^10 + 2.027323563290198480646098e-11*x^11)
Here is the plot we get:
plot(f(x) - exp(x/2 + 1/2), x)
or, after applying some of the hints regarding question 2:
plot(f(x) - exp(x/2 + 1/2), x, frame=True, figsize=(7, 2))
![Plot suffering from numerical precision issues](/upfiles/1606760028562546.png)AndrMon, 30 Nov 2020 16:06:29 +0100https://ask.sagemath.org/question/54462/Plot ratio of Bessel functionshttps://ask.sagemath.org/question/54361/plot-ratio-of-bessel-functions/I am new to SageMath and to this page. I'm running this version:
`SageMath version 9.2, Release Date: 2020-10-24 Using Python 3.8.5.`
I tried to plot the ratio of two functions related to Bessel functions.
First I defined `f` as the first derivative of `bessel_J(1, x)`
and `g`as `x*bessel_J(1, x)`:
f(x) = bessel_J(1, x)
g = derivative(f, x)
Then I defined `h` as their ratio:
h = g / (x*f)
Then, I tried to plot `h` with:
plot(h, (x, 0, 10))
The result is an empty plot, showing only the x, y axes, and this is not correct.
Am I doing something wrong? Is it possible to plot such a function and, if yes, how?wmaddoxSun, 22 Nov 2020 23:49:14 +0100https://ask.sagemath.org/question/54361/Plotting multiple functions in Sage from a for loophttps://ask.sagemath.org/question/54203/plotting-multiple-functions-in-sage-from-a-for-loop/ A while back I asked about simulating the cake eating problem in sage and the algorithm seems like an excellent way to work with things. So far the code looks as follows for obtaining a soloution to the cake eating problem:
#Step1: Name variables
k,k1,v0,beta,vk,vk1=var('k,k1,v0,beta,vk,vk1')
#Step2: Initialize your values
beta=0.6
v0=log(k)
#Step 3: The Loop to Obtain Policy function
for n in range(5):
vk = log(k-k1) + beta*v0(k=k1)
FOC = vk.diff(k1)
k1star = solve(FOC==0, k1)
print(n, k1star)
v0 = (vk).subs(k1=k1star[0].rhs())
The results I get are as follows:
0 [k1 == 3/8*k]
1 [k1 == 24/49*k]
2 [k1 == 147/272*k]
3 [k1 == 816/1441*k]
4 [k1 == 4323/7448*k]
Im interested in plotting each instance of `k1` from my loop. how would I go about doing this?EconJohnTue, 10 Nov 2020 23:20:18 +0100https://ask.sagemath.org/question/54203/plotting multiple functions from a for loophttps://ask.sagemath.org/question/46357/plotting-multiple-functions-from-a-for-loop/ This solution:
https://ask.sagemath.org/question/9024/plotting-multiple-functions-from-a-for-loop/#
is not working any more
The error is:
Error in lines 3-3
Traceback (most recent call last):
File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute
flags=compile_flags) in namespace, locals
File "", line 1, in <module>
File "/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/misc/decorators.py", line 492, in wrapper
return func(*args, **options)
File "/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/plot/plot.py", line 1956, in plot
G = _plot(funcs, (xmin, xmax), **kwds)
File "/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/plot/plot.py", line 2224, in _plot
legend_label=legend_label_entry, legend_color=legend_color_entry, **options_temp)
File "/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/misc/decorators.py", line 492, in wrapper
return func(*args, **options)
File "/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/plot/plot.py", line 1941, in plot
G = funcs.plot(*args, **original_opts)
TypeError: plot() takes exactly 1 argument (17 given)thethaThu, 25 Apr 2019 11:06:52 +0200https://ask.sagemath.org/question/46357/"triangular" sliders when plotting with @interact ?https://ask.sagemath.org/question/53431/triangular-sliders-when-plotting-with-interact/Hello! I am interested in plotting a function $f_{u}(x)$ (or $f_u(x,y)$) using @interact. The subscript $u = (u_1, u_2, u_3)$ is a vector in the $2$-dimensional simplex, so $u$ is a real-valued vector with nonnegative entries summing to $1$.
I am aware of single-variable sliders. For the plot of this function $f_u$, it seems natural to me to adjust by clicking a point in this simplex, where the simplex is drawn off to the side as an equilateral triangle with corners labeled $(1,0,0), (0,1,0), (0,0,1)$.
Are features like this built-in? I am okay to use either notebook! TheHiggsBozoSun, 13 Sep 2020 00:41:36 +0200https://ask.sagemath.org/question/53431/Plotting in 2Dhttps://ask.sagemath.org/question/53329/plotting-in-2d/I have a question about the plots made by SageMath compared to other CAS.
Why does it make the straight line between the two curves? It's like Sage is plotting the vertical asymptote without the user asking for it.
It does this to all the functions in the form of `1/x`. I have not seen this type of plotting in Mathematica or Desmos, which made me wonder why the difference. Thanks.
sage: f(x) = x^2/(x+1)
sage: plot(f, (x,-3,2),figsize=10, ymax=8,ymin=-8)
or
sage: plot(1/(x+1), (x,-3,2),figsize=10, ymax=8,ymin=-8)Alex89Sat, 05 Sep 2020 21:38:59 +0200https://ask.sagemath.org/question/53329/Plotting multiple functions with different growth rateshttps://ask.sagemath.org/question/53169/plotting-multiple-functions-with-different-growth-rates/Hi, I'm new to sagemath. I wanted to plot two different functions with different growth rates. For example, 1/x and x. I used the following code snippet. Because 1/x goes to infinity at 0, the other function can not be distinguished from the 1/x function in the plot. How can I limit plotting to specific range for example, plot only y from 0 to 10 and from 0 to 10 ?
f1 = plot((x), (x,0,2))
f2 = plot((1/x), (x,0,2))
show(f1+f2)paflerangaTue, 25 Aug 2020 14:39:04 +0200https://ask.sagemath.org/question/53169/Running the Solow Model in Sage and Plotting it.https://ask.sagemath.org/question/52545/running-the-solow-model-in-sage-and-plotting-it/As some of the regulars on this forum could tell I've really gotten into sage for its appications to economics problems. I came up withthe following code for the Solow growth model in sage.
k,k1, alpha, A, delta, s, c=var('k,k1, alpha, A, delta, s, c')
#Initial Values
k=10
s=0.5
A=5
alpha=0.6
delta=0.8
#Equations of Interest
f(k)=A*k^alpha
k1=f(k)-(1-delta)*k
invest=s*f(k)
#Visualizing the Solow Model
prod=plot(f(k),(k,1,100),color='blue')
lom=plot(delta*k,(k,1,100),color='red')
savings=plot(invest,(k,1,100),color='green')
prod+lom+savings
![image description](/upfiles/159501931381742.png)
I'm interested to see if I can make a dotted line from the x-axis and y-axis to show where the green and red lines intersect or even better display a number.
To visualize what I want I've used `microsoft paint` to aid my graphics below.
![image description](/upfiles/15950194479368789.png)
However this isn't the prettiest visualization and I'm wondering if I could get better graphics in sage. Any help is appreciated.EconJohnFri, 17 Jul 2020 23:00:20 +0200https://ask.sagemath.org/question/52545/Problem with implicit_plothttps://ask.sagemath.org/question/51443/problem-with-implicit_plot/I have three huge degree 31 bivariate polynomials (20,000 characters long each) I want to plot, but I keep getting a lot of noise in my plot. I can't upload it, but the point is that in some regions I just get colorful noise. I've tried defining the polynomials over RealField(n) and increasing the number of plot_points, but neither of these approaches work. Any ideas on how to work around this? Thanks.
**Edit:** Tried using sympy's plot_implicit and it's so (SO!) slow. Then used numpy's contour_plot and it's fast, but has the same problem as sage.
Here's the code that produces the polynomials and plot. Be patient as it could be a bit slow (depending on your machine).
plot = Graphics()
m = 32-1
D = [(i,j) for i in range(0,m+1) for j in range(0,m+1) if i+j<m+1]
#Polygon
P = Polyhedron( vertices= [(0, 0), (32, 44), (23, 0), (10, 14), (2, 3)] )
points = P.integral_points()
plot_pts = point(points, rgbcolor=(0, 0, 0), size = 20).plot()
plot_np = P.plot(fill = False, point=False, line='black')
M = matrix(ZZ, len(points), len(D), 0)
for row_num, row in enumerate(points):
for col_num, column in enumerate(D):
i, j = row
a, b= column
#Matrix for interpolation:
M[row_num, col_num] = (i^a)*(j^b)
R = PolynomialRing(QQ, 2, 'xy')
S = PolynomialRing(RealField(500), 2, 'uv')
x, y = R.gens()
u, v = S.gens()
K = M.right_kernel()
Kdim = K.dimension()
print(Kdim)
if Kdim > 0:
for l in range(Kdim):
K_basis = K.basis()[l]
#Writing the interpolating polynomial
f=0
for order, bidegree in enumerate(D):
a, b = bidegree
f += list(K_basis)[order] * u^a * v^b
F = f.factor()
f = F[6][0]
cols = ['red', 'blue', 'green']
interpolation = implicit_plot(f, (u,-1,34), (v,-1,45), plot_points=100, color=cols[l])
plot += interpolation
plot += plot_pts + plot_np
plot.show(figsize=10)
**Edit 2:** Using the mpmath library in Python and with the aid of Sébastien's code below I wrote a routine that allows us to control the root finding method and precision of our computations. I tried several methods, secant (default), newton, hailley, mnewton, etc. without success. Changing the precision and tolerance of the root finding function from mpmath didn't help either. I think this polynomial just behaves too wildly in the region of the plot.
Here's the code and relevant documentation for the "mpmath.findroot" function:
http://mpmath.org/doc/current/calculus/optimization.html :
from sympy import *
import matplotlib.pyplot as plt
import mpmath as mp
mp.dps = 100
stepx = 0.1
stepy = 0.5
xrange = np.arange(7.5,12.5, stepx)
yrange = np.arange(0, 5, stepy)
def plot_roots_of_f(f):
L = []
for u in xrange:
for v in yrange:
Root = mp.findroot(f.eval({x:u}),v, method = 'mnewton', tol = E-60, verbose=False,verify=False)
L.append([u,Root])
return plt.plot(*zip(*L),linestyle='None', marker='.')
plot_roots_of_f(f)
plt.show()jgaMon, 18 May 2020 09:30:23 +0200https://ask.sagemath.org/question/51443/How to get a nice bar chart?https://ask.sagemath.org/question/50575/how-to-get-a-nice-bar-chart/ I have
![image description](/upfiles/1586163076838346.png)
reset()
import scipy.stats
n = 2000
p = 0.2
binom_dist = scipy.stats.binom(n,p)
bar_chart([binom_dist.pmf(x) for x in range(n)], axes_labels=['$k$','$B(k,{},{})$'.format(n,float(p))])
How could I create something nicer, like
![image description](/upfiles/15861632719390012.jpg)geroyxMon, 06 Apr 2020 10:54:56 +0200https://ask.sagemath.org/question/50575/Problem with plotting "lambda" functionshttps://ask.sagemath.org/question/50229/problem-with-plotting-lambda-functions/ The following code returns AttributeError
F(x, k) = gamma_inc_lower(k/2, x/2) / gamma(k/2)
plot((lambda k: find_root(F(x, k) - 1/2, 0, 100)), (k, 1, 10))
I think there is some problem with coercion of Python function to Sage function, but I have no idea how to solve it in an elegant way.
Any solution, please?albi7Fri, 13 Mar 2020 18:20:28 +0100https://ask.sagemath.org/question/50229/Fill intersection of multiple ellipseshttps://ask.sagemath.org/question/50023/fill-intersection-of-multiple-ellipses/I'm plotting multiple ellipses via the ellipse() function. I want to only fill the area where they all overlap. I see there are many options for plotting and filling. However, all the examples I've seen refer to other types of plots that would not be compatible with ellipses. Is there a way to automatically fill the overlapping area of multiple ellipses without pre-calculating this area?
For example, two of the ellipse functions would be:
4.31814496201205*x^2 + 0.442360403122904*x*y + 0.0424448964961035*y^2 - 0.170411375833593*x - 0.0205526646997484*y - 2.4358560850288997
ellipse((0.01, 0.19), 8.86, 0.75, 92.96*pi/180)
7.81630506700337*x^2 - 47.0795923749769*x*y + 72.3295112191380*y^2 + 8.78879644990554*x - 27.0144183395227*y + 0.1289109700897777
ellipse((0.01, 0.19), 4.13, 0.17, 18.06*pi/180)mattbSat, 22 Feb 2020 19:11:41 +0100https://ask.sagemath.org/question/50023/