I new to Python/Pythonista, and I am wondering if it would be possible to have a UI to launch something I did in matplotlib. I want to be able to tap a button and launch the scatter plot. The scatter plot works without the UI, but I am having trouble getting the button to work. Here is the code I have so far:
import ui
import matplotlib.pyplot as plt
from random import choice, randint
v = ui.load_view()
v.present('sheet')
def button_tapped(sender):
show_walk()
def fill_x_values(walk_length):
x_values = [0]
points = walk_length
while len(x_values) < points:
x_direction = choice([1, -1])
x_distance = choice([1, 2, 3, 4])
x_step = x_direction * x_distance
next_x = x_values[-1] + x_step
x_values.append(next_x)
return x_values
def fill_y_values(walk_length):
y_values = [0]
points = walk_length
while len(y_values) < points:
y_direction = choice([1, -1])
y_distance = choice([1, 2, 3, 4])
y_step = y_direction * y_distance
next_y = y_values[-1] + y_step
y_values.append(next_y)
return y_values
def show_walk():
walk_length = 50000
x_values = fill_x_values(walk_length)
y_values = fill_x_values(walk_length)
plt.scatter(x_values, y_values,c='red', edgecolor='none', alpha=0.2, s=7)
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show()
button = ui.Button()
button.action = button_tapped
Do any of you see what I am doing wrong? If you need any more info from me, please let me know. Thanks in advance.