Ask Your Question

Eugene's profile - activity

2022-12-04 07:24:23 +0200 received badge  Notable Question (source)
2022-05-08 19:50:03 +0200 received badge  Notable Question (source)
2022-03-24 15:22:20 +0200 marked best answer How to put legend outside of the graph at its bottom?

Hello!

I would like to have legend located outside of the of the graph at the bottom. In the matplotlib legend guide there is an example how to locate it on the top:

# Place a legend above this subplot, expanding itself to
# fully use the given bounding box.
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
           ncol=2, mode="expand", borderaxespad=0.)

Base on that example I write in Sage Note the following code:

p = line([(0, 0), (1, 1)], legend_label='test', axes_labels=('X LABEL', 'Y LABEL'))
p.set_legend_options(bbox_to_anchor=(0., -.14, 1., .102), loc=3, mode="expand", borderaxespad=0.)
p.show(frame=True, axes=False)

bbox_to_anchor parameter is (left, bottom, width, height) , the issue is that I have to manually set bottom (-.14 in the example) to compensate for the X label (it may be present, may be missing, may have a bigger font etc.)

Is there a way to automatically put legend on the bottom just below the x-axes label?

2022-02-24 09:04:21 +0200 received badge  Famous Question (source)
2022-02-11 23:17:54 +0200 received badge  Notable Question (source)
2021-05-02 18:42:11 +0200 marked best answer line3d fonsize option?

Hi!

I need to hide exact values in 3d plot. In case of 2d I can use fontsize option like:

line([(0,0),(1,1)]).show(fontsize=0)

But show + line3d ignores fontsize option.

2021-05-02 18:38:07 +0200 received badge  Popular Question (source)
2020-07-14 08:55:19 +0200 received badge  Popular Question (source)
2020-07-14 08:55:19 +0200 received badge  Notable Question (source)
2020-07-14 08:55:19 +0200 received badge  Famous Question (source)
2020-04-30 08:54:13 +0200 received badge  Famous Question (source)
2020-01-29 07:50:58 +0200 received badge  Famous Question (source)
2019-11-25 18:04:20 +0200 received badge  Famous Question (source)
2019-11-01 10:27:59 +0200 received badge  Popular Question (source)
2019-09-05 14:40:28 +0200 received badge  Popular Question (source)
2019-09-05 14:40:28 +0200 received badge  Notable Question (source)
2019-09-05 14:40:28 +0200 received badge  Famous Question (source)
2019-06-16 06:06:56 +0200 received badge  Famous Question (source)
2019-05-22 11:15:32 +0200 marked best answer init.sage doesn't work for the notebook server?

Hello!

I have the same problem as in Is there a way to prefix all sage code in order to include a custom module?, but the init.sage appears not to work with notebook's worksheet. Here is what I have:

$ cat ~/.sage/init.sage 
import numpy as np

then inside Sage I have:

$ ./sage 
sage: np
<module 'numpy' from ....>

But then I run notebook() and create a new worksheet, inside that worksheet session:

np
NameError: name 'np' is not defined

So I assume that the script was not executed for the worksheet.

Is there a way to create sage.init but for the worksheets?

2019-05-22 11:08:04 +0200 received badge  Popular Question (source)
2018-12-23 19:21:10 +0200 received badge  Famous Question (source)
2018-10-08 00:41:21 +0200 received badge  Famous Question (source)
2018-09-20 12:22:49 +0200 received badge  Famous Question (source)
2018-08-29 04:58:27 +0200 received badge  Notable Question (source)
2018-06-05 17:57:29 +0200 received badge  Famous Question (source)
2018-03-25 13:44:53 +0200 received badge  Famous Question (source)
2018-03-25 13:44:53 +0200 received badge  Notable Question (source)
2018-02-12 14:46:14 +0200 received badge  Popular Question (source)
2018-01-02 13:50:51 +0200 received badge  Famous Question (source)
2017-10-15 20:58:41 +0200 marked best answer latex.table() function?

Can Sage construct latex code of table? Something like html.table, but with tex-output instead of html-output.

2017-10-15 20:58:41 +0200 received badge  Enlightened (source)
2017-10-15 20:58:41 +0200 received badge  Good Answer (source)
2017-08-05 10:48:08 +0200 received badge  Popular Question (source)
2017-08-01 23:21:24 +0200 received badge  Notable Question (source)
2017-08-01 23:21:24 +0200 received badge  Popular Question (source)
2017-07-14 11:09:12 +0200 received badge  Notable Question (source)
2017-05-29 21:37:23 +0200 commented question VirtualBox - Error

Something is wrong with the configuration of the VirtualBox itself? Have you moved any directories manually? Try removing C:\Users\vmuser\VirtualBox VMs\Sage-7.6\ and import again.

2017-05-29 21:34:20 +0200 marked best answer How to efficiently calculate a sum of arrays with numpy and @parallel decorator?

Hello!

I have an algorithm to process a huge array by chunks. Each processing operation results in a matrix of size N*N, I need to calculate a sum of these matrices. For simplicity assume processing function does almost nothing and requires no input - just returns zeros. In that case working example looks like this:

import datetime
import numpy as np
import time

N = 1024 * 2
K = 256

def f():
    return np.ones((N, N),  dtype=np.complex128)

buffer = np.zeros((N, N),  dtype=np.complex128)

start_time = datetime.datetime.now()

for i in range(K):
    buffer += f()

print 'Elapsed time:', (datetime.datetime.now() - start_time)

Execution takes about 5 seconds on my PC. Now, as function f becomes more complex, I would like to run in parallel, so I modify code as follows:

import datetime
import numpy as np

N = 1024 * 2
K = 256

@parallel
def f(_):
    return np.ones((N, N),  dtype=np.complex128)

start_time = datetime.datetime.now()

for o in f(range(K)):
    buffer += o[1]

print 'Elapsed time:', (datetime.datetime.now() - start_time)

And now it takes about 26 seconds to calculate! What am I doing wrong? Or what causes such a huge overhead? (it looks silly for if the cost of collecting the result of f() across parallel processes is more than calculating one iteration of f() itself, I better run f() without parallelism at all)

2017-04-22 09:43:01 +0200 commented question UnicodeDecodeError in Notebook Server if Worksheet is set to 'python' instead of 'sage'

Learned about Jupyter for real last evening, bot that problem and some other problems are not present in Jupyter (and Jupiter seems prettier, not sure about the Jupyter's markdown vs Sage's rich text boxes and Jupyter's versioning vs Sage's versioning thought). I think because of Jupyter this question is outdated, and the related github issues may also be closed in order not to draw developers attention and save time. Still thanks :)

2017-04-21 21:24:54 +0200 asked a question UnicodeDecodeError in matplotlib if 'python' set instead of 'sage' in Notebook

Hello!

I am trying to make publication ready images for LaTeX in Sage with matplotlib (due to the complex nature of the plot) in Notebook.

I have a lot of numerical calculations (not symbolic) so I switch to 'python-mode' using drop-down list on top of the Worksheet (so it looks like 'File...', 'Action...', 'Data...', 'python') so that my constants would read as native python's data types not Sage's symbolic objects.

Now having the switch on top of the page in 'python' mode the following code:

from matplotlib import rc

rc('text', usetex=True)
rc('text.latex', unicode=True)
rc('text.latex', preamble='\usepackage[utf8]{inputenc}')
rc('text.latex', preamble='\usepackage[russian]{babel}')

font = {'family': 'serif',
        'serif': ['Computer Modern Unicode']}

rc('font', **font)

import matplotlib.pyplot as plt
plt.plot([1], [1])
plt.title(ur'Тест')
plt.savefig("test.png")

yields ether UnicodeDecodeError or the image with the text corrupted: example of the corrupted output

But if I switch to 'sage' on top of the page, it works as expected:

example of the valid output

I have no idea how that switch on top of the page affects matplotlib's output, but would really like to be able to use Cyrillic (utf8) letters and 'python' mode at the same time.

P.S. That magical switch on top of the page is really painful since I also can not save Worksheets if they contain utf-8 characters: as in this still unresolved issue :-( :-(

UPDATE: Tested the MWE above in Jupiter - seems to work properly! And due to inevitable migration to Jupiter, the problem seems not to be obsolete.

2017-04-12 15:55:38 +0200 received badge  Notable Question (source)
2017-03-15 20:22:53 +0200 asked a question Sage Math future (notebook vs cloud vs jupiter)?

Hello!

For some years I have used Sage but only on my local PC and via notebook. Now I am noticing that Sage Cloud becomes more popular, it has jupiter and the interface differs a lot.

So, I have a some general questions about the future of Sage Notebook, Sage Cloud and jupiter:

  1. Is using Sage Notebook is the 'recommended' way of using sage offline (or I should google about jupiter)?
  2. Will something like Sage Cloud Docker replace the use of Sage's binary and Sage's Notebook in the future?
2017-02-15 13:37:51 +0200 received badge  Popular Question (source)
2017-02-04 22:23:06 +0200 received badge  Self-Learner (source)
2017-02-04 21:14:26 +0200 answered a question How to efficiently calculate a sum of arrays with numpy and @parallel decorator?

Workaround: use sharedmemory and multiprocessing packages (sharedmemory is available via pip). I tried the following code within a separate .py file:

import sharedmem
import multiprocessing as mp
import numpy as np

N = 1024 * 2
K = 256

_lock = mp.Lock()


def f(_shared_buffer):
    partial_result = np.ones((N, N), dtype=np.complex128)
    _lock.acquire()
    _shared_buffer += partial_result
    _lock.release()


def test():
    shared_buffer = sharedmem.empty((N, N), dtype=np.complex128)
    pool = mp.Pool(4)
    pool.map(f, [shared_buffer for _ in range(K)])
    return shared_buffer

Advantages of this workaround:

  • Executes as fast as the original code with no parallelism (about 5 seconds on my PC)

Disadvantages:

  • Requires usage of the additional package sharedmem (although this package is available via pip)
  • I was not able to pass _lock object to f() from within Sage's Notebook and have to put the code in a separate file, than import and only then execute
  • The buffer from np.ndarray becomes sharedmem.sharedmem.anonymousmemmap (not sure if that is a disadvantage, I guess one can convert it back to numpy's type pretty fast)
2017-02-04 21:03:46 +0200 commented question How to efficiently calculate a sum of arrays with numpy and @parallel decorator?

Thanks for the note about shared memory! I think that is the case here, I found a workaround with sharedmem and multiprocessing modules, gonna post it as an answer.

2017-02-04 12:33:32 +0200 asked a question Multiple tables in Notebook's output?

Hi!

Trying to print more than one table in the output of Sage's Notebook cell, with fancy-printing:

table([[0]])
table([[1]])

but only the last one is shown (checked in Firefox and Chrome in Linux). Currently found workaround is:

html.table([[0]])
html.table([[1]])

but it gives a deprecation warning, or:

html(table([[0]])._html_() + table([[0]])._html_())

which seems to work fine.