Display some points by mouse in a plot

asked 2020-06-26 03:45:00 -0500

Cyrille gravatar image

updated 2020-06-27 23:32:19 -0500

This is the code for the plotting what economist call the portfolio frontier

pp=parametric_plot((s(x),x), (x,-5, 6), fill=True,color='grey',alpha=0.01,axes_labels=['$\sigma_p$','$\mathbb{E}_p$'],axes_labels_size=1)
pp+=parametric_plot((s(x),x), (x,0, 6),color='red',)
pp+=parametric_plot((s(x),x), (x,-5, 0),linestyle='--',color='red',)
pp+=points([(5,1),(10,2),(20,-2)],color='blue')
pp+=text("$Pechiney$", (5,.6), alpha=1, fontsize='xx-small', fontweight='bold', color='green')
pp+=text("$LVMH$", (10,1.6), alpha=1, fontsize='xx-small', fontweight='bold', color='green')
pp+=text("$Peugeot$", (20,-2.4), alpha=1, fontsize='xx-small', fontweight='bold', color='green',)
show(pp,aspect_ratio='automatic')

I wonder

1) if it would be possible to display the point and there label, only when the mice is on them ?

2) to click on the grey part of the graph, to make appear a point which display its coordinates ?

3) I have tried some tricks to supress the "DeprecationWarning" which arise with this code without success. Why does it appears, how to suppress it ?

NB There is no accessible points outside of the grey zone.

Add I have seen a Python package PyAutoGui. I would like to know if anay body's has an experimetn of it's interaction with Sage.

edit retag flag offensive close merge delete

Comments

For the deprecation warning, use raw strings when your label contains \ by adding the letter r before the string, for example r"$\mathbb{E}$"

FrédéricC gravatar imageFrédéricC ( 2020-06-26 06:24:26 -0500 )edit

Thanks. The problerm with r is that sometimes it must be placed before the command which must be 'rowed' sometimes after.

Cyrille gravatar imageCyrille ( 2020-06-26 13:09:20 -0500 )edit

look at this web page: event_handling

maybe that will help you, (not sure), but the examples don't work in Jupyter notebook, only with sage in terminal.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

fig, ax = plt.subplots()
ax.plot(np.random.rand(10))

def onclick(event):
    print('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
          ('double' if event.dblclick else 'single', event.button,
           event.x, event.y, event.xdata, event.ydata))

cid = fig.canvas.mpl_connect('button_press_event', onclick)
show(cid)
ortollj gravatar imageortollj ( 2020-06-28 11:35:04 -0500 )edit

in the examples above x, y are displayed at the bottom of the figure. I know it's not quite what you wanted ;-(

ortollj gravatar imageortollj ( 2020-06-28 11:39:05 -0500 )edit