• chjl07

    @Phuket2

    Thanks again. So finally it worked. Here the total code. This plots the parabolic trajectory as a function of angle and initial velocity. I just need to figure out how to delete the previous image and not have them superimposed when the sliders are used.

    import matplotlib.pyplot as plt
    import numpy as np
    import ui
    from io import BytesIO
    
    
    g=9.81
    
    def plotparabole(alpha,v_0):
    #Le programme calcule en radians, mais un input en degres est plus intuitif    
        alpharad=alpha*np.pi/180
    #le graphe sera de 100 points, pour un temps entre 0 et 9 secondes, 
    #mais suivant les conditions, le temps de vol réel peut être plus court    
        t = np.linspace(0,9,100)
    #représentation du vecteur vitesse à t=0
        vit=[0,0,v_0*np.cos(alpharad),v_0*np.sin(alpharad)]
        ax = plt.gca()
        ax.quiver(0,0,v_0*np.cos(alpharad),v_0*np.sin(alpharad),color='red',angles='xy', scale_units='xy', scale=1)
        plt.axis([0, 170, 0, 100])
    #tracé de la trajectoire, grâce à des coordonnées parametrées en fonction du temps    
        plt.plot(v_0*np.cos(alpharad)*t, -0.5*g*t**2+v_0*np.sin(alpharad)*t)
        plt.axes().set_aspect('equal')
        b = BytesIO()
        plt.savefig(b)
        img = ui.Image.from_data(b.getvalue())
        return(img)
    
    
    #########################################################
    
    
    
    def slider_action(sender):
        # Get the root view:
        v = sender.superview
        # Get the sliders:
        v0 = v['slider1'].value*40
        alpha = v['slider2'].value*90
        v['alpha'].text = 'alpha=%f degres' % (alpha)
        v['v0'].text = 'vitesse=%f m/s' % (v0)
        img=plotparabole(alpha,v0)
        v['imageview1'].image=img
        
    
    
    v = ui.load_view('Balistique')
    slider_action(v['imageview1'])
    if ui.get_screen_size()[1] >= 768:
        # iPad
        v.present('sheet')
    else:
        # iPhone
        v.present()
    

    and here the pyui file:

    [
      {
        "selected" : false,
        "frame" : "{{0, 0}, {560, 427}}",
        "class" : "View",
        "nodes" : [
          {
            "selected" : false,
            "frame" : "{{15, 380}, {242, 34}}",
            "class" : "Slider",
            "nodes" : [
    
            ],
            "attributes" : {
              "flex" : "W",
              "border_width" : 1,
              "action" : "slider_action",
              "frame" : "{{180, 168}, {200, 34}}",
              "border_color" : "RGBA(1.000000,0.200000,0.200000,1.000000)",
              "class" : "Slider",
              "value" : 0.5,
              "uuid" : "0EBE87B6-3527-4D66-B9D6-F1361D7BB24F",
              "corner_radius" : 2,
              "name" : "slider1"
            }
          },
          {
            "selected" : false,
            "frame" : "{{295, 387}, {232, 34}}",
            "class" : "Slider",
            "nodes" : [
    
            ],
            "attributes" : {
              "action" : "slider_action",
              "flex" : "W",
              "frame" : "{{180, 168}, {200, 34}}",
              "uuid" : "6D51E0F1-9BE3-4486-8962-D365342746A5",
              "class" : "Slider",
              "value" : 0.5,
              "name" : "slider2"
            }
          },
          {
            "selected" : false,
            "frame" : "{{295, 340}, {198, 32}}",
            "class" : "Label",
            "nodes" : [
    
            ],
            "attributes" : {
              "font_name" : "<System>",
              "frame" : "{{205, 169}, {150, 32}}",
              "uuid" : "733AE3FF-A92D-41CD-B37D-66EF27B4DD3A",
              "class" : "Label",
              "alignment" : "left",
              "text" : "alpha",
              "font_size" : 18,
              "name" : "alpha"
            }
          },
          {
            "selected" : false,
            "frame" : "{{15, 340}, {178, 32}}",
            "class" : "Label",
            "nodes" : [
    
            ],
            "attributes" : {
              "font_name" : "<System>",
              "frame" : "{{205, 169}, {150, 32}}",
              "uuid" : "733AE3FF-A92D-41CD-B37D-66EF27B4DD3A",
              "class" : "Label",
              "alignment" : "left",
              "text" : "v0",
              "name" : "v0",
              "font_size" : 18
            }
          },
          {
            "selected" : true,
            "frame" : "{{15, 28}, {524, 304}}",
            "class" : "ImageView",
            "nodes" : [
    
            ],
            "attributes" : {
              "alpha" : 1,
              "frame" : "{{230, 164}, {100, 100}}",
              "class" : "ImageView",
              "background_color" : "RGBA(0.956522,1.000000,0.869565,1.000000)",
              "uuid" : "60FC8613-07A2-4229-ACFE-B8DD87CC03FE",
              "name" : "imageview1",
              "image_name" : "iob:alert_256"
            }
          }
        ],
        "attributes" : {
          "enabled" : true,
          "background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
          "tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
          "border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
          "flex" : ""
        }
      }
    ]
    

    posted in Pythonista read more
  • chjl07

    Sorry, I was too fast typing my last message. I meant :

    x = np.linspace(0,9,100)
    img=plt.plot(x,np.sin(x))
    plot.show()
    v['imageview1'].img=img
    

    the plot.show shows the curve in the background. But I have no curve in the user interface

    posted in Pythonista read more
  • chjl07

    Hi Phuket2
    Thanks a lot. your example works on my ipad and I think this approach could work. I have been using the approach of designing the ui with pyui, right now I am still trying to figure out how to pass the image to the ui...

    x = np.linspace(0,9,100)
    plt.show()
    img=plt.plot(x,np.sin(x))
    v['imageview1'].img=img
    

    doesnt deliver any error, but doesnt do the job either...

    posted in Pythonista read more
  • chjl07

    Hello!
    I am new to pythonista but found very interesting this approach of turning an ipad into a programmable device...

    I would love to be able to use it for some teaching activities, but I would need to be able to plot a curve that is changed dynamically by moving sliders.
    I can plot the curve in pythonsta
    I can do sliders in pythonista
    I can plot the dynamic curve with other python interfaces (jupyter notebook, using interact from ipywidgets)
    but I wasn't able to find any indication if that would be possible with pythonista.

    So is it possible and if yes, is there an example somewhere?

    Thanks for any help

    posted in Pythonista read more

Internal error.

Oops! Looks like something went wrong!