A similar question was asked here and here on Stack, but I'm not satisfied as it still did not resolve my issue.
Consider I have a function which does some calculations and displays a figure (without returning a figure object). Out of personal preference (and to keep track of what my plots looked like without saving each) I use %matplotlib inline in IPython. Now after I generated some plots I decide to save one of them (say the second out of 3 displayed in the notebook), which works fine by right clicking and choosing 'Save as...', but only as .png.
Is there a way to save it as .pdf without modifying the function to return a figure object? (I know it is not difficult at all, but for most of my cases it is just unnecessary since it is 1 out of, say, 20 figures worth saving in the end).
I figured out that the backend is being changed after %matplotlib inline which is (I guess) the reason why I can not save figures as .pdf. The workaround seems to be using %config InlineBackend.close_figures = False and using plt.savefig(...)(answer from here). But this way I could save only the last figure and have to close the figures manually each time.
If my problem arises from bad program workflow / programming style, I will happily accept suggestions on how to do it better. If a code example is needed, I can provide one.
I use:
ipython (2.1.0)
matplotlib (1.4.1) (with Qt4Agg backend if not inline)
Python 2.7.6
MacOSX 10.9.4
Reposting as an answer:
You can't save the PDF directly from your browser, because the browser and JS code doesn't know that the PNG image it displays is associated with a particular matplotlib figure - indeed, it doesn't know about matplotlib at all. All that is handled by the Python kernel process running your code.
However, you can configure IPython to display figures in SVG format, as described in the docs. It also appears to have a PDF option, though I forget what that does.
Related
I've been making the switch over to VSCode lately, and have had quite a lot of success, I love the format. However, one thing I'm sorely missing is a good variable viewer for arrays and lists.
I am aware of the variable viewer that you can look at during debugging, however it's horribly and unnecessarily cluttered for lists and numpy arrays -having 4 sub folders- and you have to click into two of them to view the array laid out in a reasonable fashion. But with arrays wider than 4 you have to constantly scroll. Worse, while stepping through it will often close those subfolders and you have to reopen them again to continue watching!
Is there any way to view the array in a separate window that updates on its own (Yes I found the "data viewer" but you have to refresh it for it to update to any changes that have occurred to the array) like the way you can in Spyder or MATLAB?
Thanks!
Here's images of what I've found:
This is the variable viewer I've already found, as you can see, it has all that cluttering with multiple folders I was talking about. I only need to see the values under the "0", "1", "2" subfolders because I need to watch them update as I step through my code.
I did find this data viewer, but it doesn't update as I step through the code, it has to be manually refreshed.
Spyder has this variable viewer, which is the sort of thing that I'm looking for. It automatically updates as you step through your code and you can resize the variable slots (like you would adjust the size of a cell row/column in excel) to fit your needs
I think you have already found the outline view so I won't go over that.
Another way -
You could go to the debug console and output it to there.
Last way -
Right-click "Run Current File in Python Interactive Window" in the script, and select "Show variables active in jupyter kernel":
Some data/value representations are working as expected when using Jupyter Notebook "native" support in VSC, but it seems that JavaScripts elements are not properly represented or previewed.
When I try to get Map (gmaps) as a figure, in VS Code, as a result, I get Figure(layout=FigureLayout(height='420px')) whereas when I execute the same code block in Jupyter Notebook/Lab server inside of Chrome browser, I get the wanted output which tells me that my external setup should be Ok. I'm using jupyter-js-widgets and jupyter-gmaps extensions.
I have a feeling that I'm missing some additional configuration(s) in VS Code regarding JavaScripts.
My VS Code About:
My Jupyter Version
Thank you
#MatejZ. Currently most widget based Jupyter extensions won't work in VSCode Notebook Editor and Interactive Window. However we are looking at add more support for this as it's a highly requested feature. If you want you can track our GitHub issue for this support (that we've already started to look into) here:
https://github.com/microsoft/vscode-python/issues/3429
Temporary solution was to add something like this:
# create html
from ipywidgets.embed import embed_minimal_html
embed_minimal_html('export.html', views=[fig], title = 'Gmap output')
which will take object "fig" and make it accessible through "export.html" page.
Thank you #Ian Huff for pointing to the issue :) Webview was mentioned in the issue, which could be coupled with the temp solution. But, I'm satisfied for now.
I'd like to take advantage of a number of features in PyCharm hence looking to port code over from my Notebooks. I've installed everything but am now faced with issues such as:
The Display function appears to fail hence dataframe outputs (used print) are not so nicely formatted. Equivalent function?
I'd like to replicate the n number of code cell in a Jupyter notebook. The Jupyter code is split over 9 cells in the one Jupyter file and shift+ Enteris an easy way to check outputs then move on. Now I've had to place all the code in the one Project/python file and have 1200 lines of code. Is there a way to section the code like it is in Jupyter? My VBA background envisions 9 routines and one additional calling routine to get the same result.
Each block of code is importing data from SQL Server and some flat files so there is some validation in between running them. I was hoping there was an alternative to manually selecting large chunks of code/executing and/or Breakpoints everytime it's run.
Any thoughts/links would be appreciated. I spent some $$ on Udemy on a PyCharm course but it does not help me with this one.
Peter
The migration part is solved in this question: convert json ipython notebook(.ipynb) to .py file, but perhaps you already knew that.
The code-splitting part is harder. One reason to why Jupyter is so widely spread is the functionality to split the output and run each cell separately. I would recommend #Andrews answer though.
If you are using classes put each class in a new file.
I'm using Ipython Notebook to do some data analysis and i'd like to be able to "upload" (really just copy) data files inside the Json Ipython notebook.
This way the Ipython notebook is self contained and doesn't depend on a external file position
As an example
with open("Anyfile.dat", "r") as PlainImageFile:
Would be something like
with fromNotebook("Anyfile.dat") as PlainImageFile:
No it is not possible, and it might not even make sens, as the notebook itself might not be on the machine where the kernel runs.
This is not either the goal of the Jupyter/IPython notebook format.
Moreover the kernel as no notion of wether it is runned form inside a notebook, by design. So where would fromNotebook look for the data ?
The closest to what you ask is activePapers from Konrad Hinsen, it is a fully contained environment (that also work with notebooks) ment to be fully self contained.
The second option, is to say : 1 git repository for 1 project. You can add data, images, library. Hashdist also help with that.
At least in IPython-2 it is possible to implement an Upload Widget.
See the examples in these ipynb-notebooks:
Upload-widget for IPython-2.
Upload-widget for IPython-3
I know the python interpreter and ipython have a easy way to browse through the history of commands. That is in interactive Python programming.
My problem/question:
I have a GUI-based Python tool that allows me to click and enter values in fields before hitting the "PLOT" button and I get a plot on screen. What I am looking for is a way to access a "minimimum script" that exactly reproduces the plot.
So I was wondering if there was a way to request a backlog of all the commands an uninteractive Python instance went through.
If it is not built-in, could someone advise a way to automatically dump function calls in a file at the same time as they are run.
The simplest way to do that would be to Pickle your plot object. Then you can just reload the pickle file and the object will be in memory just as it was when dumped.
It should only take a couple of lines to implement a dump and reload feature in your program.
This of course doesn't give you a list of commands or anything like that to regenerate the figure, but it does give you the exact state of the object.
If you are using matplotlib to do the plotting, then the image itself is not picklable. But you could create a class that contains all the information you entered that is passed to the matplotlib routines and pickle that, again saving the state.