Matplotlib, Jupyter: Have annotation appearing when hovering over plot - python

I am trying to create a plot with matplotlib that has tooltip appearing when hovering hover certain elements of the charts. I want to do that in the Jupyter Notebook, with python 3.
I have tried the snippets provided here:
http://matplotlib.org/examples/event_handling/pick_event_demo.html
Possible to make labels appear when hovering over a point in matplotlib?
Is there a matplotlib equivalent of MATLAB's datacursormode?
Matplotlib basemap: Popup box
But none of them work. The plot shows but is not interactive.
I tried with and without the magic %matplotlib inline with no effect.
I of course also modified the print statement when it was without parenthesis.
Is there a specific command or backend to use in order to have the interaction working in Jupyter?
EDIT:
the example working with mpld3 "works" but i would rather stick to the basic matplotlib.

I think you want %matplotlib notebook. This will integrate interactive plots in the notebook.

Related

Problem with the display of the heatmap on jupyter: it crushes the old graphs of other measures

when I display the distributtion plot it works just fine and I get my graph displayed.
sns.distplot(df['Price'])
when I add a new code line to display the heatmap of the corr:
sns.heatmap(df.corr())
it Crushes my old graph and get displayed at its place.
can't get both graphs displayed at the same time.
You can try displaying multiple images in Jupyter notebooks with the IPython.display.display() method. IPython is the python shell used by Jupyter notebooks, so you can import it without installing any external libraries. Just wrap your plots inside the function like you would with a print method:
from IPython.display import display
display(sns.distplot(df['Price']))
display(sns.heatmap(df.corr()))

converting matplotlib figure into a plotly figure

I am currently working in a project where i need to display Enthalpy/Pressure diagram which look like this:
Diagram enthalpy/Pressure
I am doing it with matplotlib & Coolprop using this kind of code:code diagram
I want an interactive figure where i can see the values of all the points i draw just by passing my mouse on them.
I know that plotly allow this kind of thing, so my question is how can i convert my matplotlib figure into a plotly one or how can i change my code to use plotly instead of matplotlib ?
Thank you

Visualizing Python interactive plots outside of Jupyter

I am making some small tests in Jupyter. The user should see two plots, one on the left and one on the right. Than after clicking somewhere on the left plot something on the right plot should happen. For example here a click on the left plot will produce a red dot on the right plot in the same place:
%matplotlib notebook
def onclick(event):
ax.clear()
ax2.clear()
ax.set_xlim(0,10)
ax.set_ylim(0,10)
ax2.set_xlim(0,10)
ax2.set_ylim(0,10)
ax2.plot([event.xdata],[event.ydata],'o',color='red')
ax.figure.canvas.draw_idle()
ax2.figure.canvas.draw_idle()
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax.set_xlim(0,10)
ax.set_ylim(0,10)
ax2.set_xlim(0,10)
ax2.set_ylim(0,10)
cid = fig.canvas.mpl_connect('button_press_event',onclick)
This works, but the visualization in Jupyter is not the best. Is there a way to have this kind of interactive plots working outside of a Jupyter notebook ? Maybe in a window with varying dimensions ? As an example, how would one proceed with the example above ?
You can use plotly and export your plots to html file:
https://plotly.com/python/interactive-html-export/
I think I found a possible simple answer to my needs. Simply put the code in the question in a python script, remove the 'magic' command and insert at the end:
plt.show(block=True)
This way matplotlib will just use a different backend than notebook or inline in Jupiter and plot the figure in a separate window. This window is compatible with the interactive commands from matplotlib. I am not sure about widget compatibility though I will try and update the answer.
Update: I do not think ipywidgets can be used with plt.show() since these are not plotted in an axes, but there are some widgets within matplotlib (matplotlib.widgets) that can be used. Though, the resulting speed in my case is not satisfactory so I would avoid combining matplotlib widgets with matplotlib event catching.

How to create an interactive heatmap with Python?

I'm trying to create an interactive Heatmap in Jupyter :
I want to be able to display a graph when double-clicking on the different cells. For instance when clicking on the cell corresponding to correlation between A and B I want to display a graph showing corr(A,B) across time.
It appears that neither plotly nor seaborn allows me to do that. Any help is well appreciated !

Matplotlib and Jupyter notebook multiple interactive plots

I am facing the following problem: I need two interactive plots to work at the same time on Jupyter, however they are interfering. When I rotate the cell of the first, the second plot stops being interactive and becomes "inline". Other times one of the plots looks like this:
The expected result was something like this
I imagine the problem is in the implementation I made. As you can see below, I use plt.something to put things in the figure (both for plot 1 and plot 2).
I'm using the %matplotlib notebook environment and tried to implement using fig1, ax1 = plt.subplots(). I would like to know if it is possible to do this type of implementation, where there is no conflict between plots? Maybe I am using matplotlib badly, so I would like some suggestions.

Categories

Resources