In [ ]:
#gauss's circle problem
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import math
#fig = plt.figure()
#fig, ax = plt.subplots(1, 1)
fig = plt.figure()
ax = fig.add_subplot(111)
#want to run through n=x^2+y^2 for n = 0,1,2,3...
n=3
#if not ('n' in locals()):
# n = 0
ax.set_xticks(np.arange(-11,12,1))
ax.set_yticks(np.arange(-11,12,1))
#plt.scatter(x,y)
circ=plt.Circle((0,0), radius=math.sqrt(n), color='black', fill=False)
#plt.gca().add_patch(circ)
#print type(plt.gca())
ax.add_patch(circ)
# add x and y axis
plt.axis([-10, 10, -10, 10])
plt.grid()
##########################################################################
axcolor = 'green'
#'underlying' color of slider bar is green. when you slide it, it always gets covered by black
axfreq = plt.axes([.2, 0.025, 0.65, 0.03], axisbg=axcolor)
#the abvoe describes the layout of hte sliders on the screen
sfreq = Slider(axfreq, 'Freq',0, 10.0, valinit=0)
#5 is the min value of hte slider, 30 is the max, f0=3 is initial value of slider position
# can i amke slider step sizes integers?
########################
def update(val):
#amp = samp.val
freq = math.floor(sfreq.val) # sfreq is the slider, .val is its current value!
#l.set_ydata(a0 + f0*t)
n=freq
circ.radius = freq
dx = []
dy=[]
# one issue: yo uneed to delete the old rectangles/circle before adding new one
lower = int(math.floor(math.sqrt(n)))
# build lists dx, dy below
for a in range(-lower,lower+1,1):
for b in range(-lower,lower+1,1):
if (a**2+b**2) <= n:
dx.append(a)
dy.append(b)
# add a bunch of little unit squares for points inside circle
for coord in zip(dx,dy):
rectangle = plt.Rectangle(coord, 1, 1, fc='green',alpha=.2)
ax.add_patch(rectangle)
ax.scatter(dx, dy, s=10, color='black')
fig.canvas.draw_idle()
sfreq.on_changed(update)
#samp.on_changed(update)
################################################################################
### its almost as if fig.draw_idle() will redraw the figure with it's attributes changed.
#but they must be attributes of the figure. they can just be any old global variables.
# so i could either try to beat hte system and change hte global variables ,or somehow
# just make the changes via the attributes (e.g. change radius vs change n) (or change
#paramaters of scatter plot ?)
plt.show()
range(-10,10,1)
In [33]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 3
s = a0 + f0*t
l, = plt.plot(t, s, lw=2, color='blue')
plt.axis([0, 1, -10, 10])
####################################################################
axcolor = 'green'
#'underlying' color of slider bar is green. when you slide it, it always gets covered by black
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
#axamp = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)
#the abvoe describes the layout of hte sliders on the screen
sfreq = Slider(axfreq, 'Freq',5, 20.0, valinit=f0)
#5 is the min value of hte slider, 30 is the max, f0=3 is initial value of slider position
# can i amke slider step sizes integers?
#samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
#########################################################################
def update(val):
#amp = samp.val
freq = sfreq.val # sfreq is the slider, .val is its current value!
l.set_ydata(a0 + freq*t)
fig.canvas.draw_idle()
sfreq.on_changed(update)
#samp.on_changed(update)
#resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
#button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
#print type(l)
plt.show()
In [30]:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider
class ChangingPlot(object):
def __init__(self):
self.inc = 0.5
self.fig, self.ax = plt.subplots()
self.sliderax = self.fig.add_axes([0.2, 0.02, 0.6, 0.03],
axisbg='yellow')
self.slider = Slider(self.sliderax, 'Value', 0, 10, valinit=self.inc)
self.slider.on_changed(self.update)
self.slider.drawon = False
x = np.arange(0, 10.5, self.inc)
self.ax.plot(x, x, 'ro')
self.dot, = self.ax.plot(self.inc, self.inc, 'bo', markersize=18)
def update(self, value):
value = int(value / self.inc) * self.inc
# new value on slider is obtained from old value
self.dot.set_data([[value],[value]]) # i think just specifying new posotion of big ball
#self.slider.valtext.set_text('{}'.format(value))
# line above makes slider display nice numbers only
self.fig.canvas.draw()
def show(self):
plt.show()
p = ChangingPlot()
#p is an object thats just been created from a class!
print type(p)
p.show()
In [ ]: