pydev autocomplete for matplotlib - matplotlib

Here is the code I write in pydev combined with eclipse.
import matplotlib.pyplot as plt
fig=plt.figure()
as I know, 'fig' is a instance of 'matplotlib.Figure' class,when I write :
fig.
it seems pydev can't provide method calltip for fig. I cannot figure out what's going on, since for other module , like numpy, it works well.by the way, if i use a matlab-like interface, for example,
plt.plot()
pydev does provide the calltip for function arguments.
is there a way to solve this problem? I will appreciate it if anyone give a solution .
Forgive my poor english:-D

fig is an instance of matplotlib.figure.Figure so what you can do is importing import matplotlib.figure and creating an instance of that. Then, writing fig to the editor, you should get the tooltip you want.
The following is a screenshot from Spyder, so I haven't actually tested it in pydev.
I am not aware of any other possibility. The reason is that for the requested functionality to work the editor would need to load all kinds of modules, which are not actually imported in the script.

The issue is that some cases are too dynamic for PyDev to know about the actual type of the object that some method returns (which appears to be the case).
If you know the type, you can manually type it locally.
i.e.: Add the comment:
#: :type fig: matplotlib.figure.Figure
right before the fig assignment.
See: http://www.pydev.org/manual_adv_type_hints.html for more details.

Related

Get auto import completions without adding import statements

I have python embedded into my program and added a few functions using the Python C API. I'd like to get PyLance IntelliSense providing autocompletion for it.
After setting up paths to a dummy python file (with dummy functions), PyLance seems to suggest auto completions for my functions but also adds the top-level from x import y statements. I've tried to set the python.analysis.autoImportCompletions to false but it disables the autocompletion entirely.
So, how to get the autocompletion without PyLance adding the import statements at the top? (I know it's not practical but I think it suits my use case here)

How to save an interactive plot produced by matplot

I got a super graph that I would like to export as an html file to be showed on an website but I don't know how to figure that. Have you got an idea ?
You can use mpld3. This is a great tutorial. The mpld3 library's main functionality is to take an existing matplotlib visualization and transform it into some HTML code that you can embed on your website.
Use fig_to_html file, which accepts a matplotlib figure object as its sole argument and returns HTML.
To use the fig_to_html method , simply add the following code to the end of our Python script:
html_str = mpld3.fig_to_html(fig)
Html_file= open("index.html","w")
Html_file.write(html_str)
Html_file.close()

Trying to view decision tree in my notebook

I am trying to scale my decision tree to fit notebook but it appears not to scale properly. I have to keep scrolling for a better view. Can I please have some help on how to fix this. Attach is a pic of how it looks like.
from graphviz import Source
from sklearn import tree
from IPython.display import SVG
graph = Source( tree.export_graphviz(dt_classifier, out_file=None, feature_names=X.columns))
SVG(graph.pipe(format='svg'))
Perhaps it's not relevant any more, since this question has been open for about six months now. However, I just stumbled into it, as apparently 83 other readers, and I just crafted my way around this. The easy way is to use the pydot package (pip install pydot), and then add the default size. I have also been using %matplotlib inline so that it displays nicely within the notebook but without using the svg module. With your example:
%matplotlib inline
from graphviz import Source
from sklearn import tree
import pydot
dot_data = tree.export_graphviz(dt_classifier, out_file=None, feature_names=X.columns))
pdot = pydot.graph_from_dot_data(dot_data)
# Access element [0] because graph_from_dot_data actually returns a list of DOT elements.
pdot[0].set_graph_defaults(size = "\"15,15\"")
graph = Source(pdot[0].to_string())
graph
I also added rotate=True to export_graphviz so that it displays in horizontal style, the root of the tree is directly visible, and is easier to follow. Of course, you can play around with size so as to reach something that is acceptable for you.

Visualising numpy arrays in PyCharm

I'm trying to use PyCharm for developing and debugging numpy code, and I would like to have a similar tool for inspecting numpy arrays. According to this link https://www.jetbrains.com/pycharm/features/scientific_tools.html there is built-in numpy array viewer. However, when stopped in the debugger, all I could see is this:
which is a really low-level representation. Is this really what they mean by the "array viewer" or there is the feature hidden in somewhere.
Thanks!
To view a NumPy array, run your project in a debug mode and find the NumPy array in the variables list shown in the PyCharm`s graphical debugger. Right-click it and select “View as Array”.
It should look like this (not the low-level representation you're seeing.):
Source: https://blog.jetbrains.com/pycharm/2014/11/third-pycharm-4-eap-numpy-array-viewer-ipython-notebook-improvements-and-more/
If a numpy array is an image and you want to see its content visually, you can use OpenCV Image Viewer Plugin, which I've just released. It works with any JetBrains IDE, which support Python (directly or via plugin).
https://plugins.jetbrains.com/plugin/14371-opencv-image-viewer

Pylab / matplotlib magic in IPython: supress loading message

Currently in IPython, when you call %pylab inline or %matplotlib inline the following message displays under the code block.
"Populating the interactive namespace from numpy and matplotlib"
How I can suppress this message from being displayed?
I don't think there's a builtin way of suppressing that message since if you look at the %pylab magic function in this file you can see that the print statement is hard coded in there.
If this is a one-off kind of thing you can simply comment/remove that print line from your local library. (Typically it would be found at /usr/local/lib/python2.7/dist-packages/IPython/core/magics/pylab.py.) Or possibly redirect stdout to devnull or something like that.