I can't add any sliders for plot to adjust polygon coordinates.
all sliders should only move the points along the x-axis.
import numpy as np
from matplotlib.patches import Polygon
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
# Define initial parameters
bl = 200 #bottom left point
tl = 400 #top left point
tr = 600 #top right point
br = 1080 #bottom right point
# Create the figure and the line that we will manipulate
img = 'test1.jpg'
im = plt.imread(img)
fig, ax = plt.subplots()
im = ax.imshow(im, extent=[0, 1080, 0, 720])
pts = np.array([[bl,0], [tl,340], [tr,340], [br,0]])
p = Polygon(pts, closed=True, alpha=0.4, fc='green', ec="red")
ax = plt.gca()
ax.add_patch(p)
# adjust the main plot to make room for the sliders
fig.subplots_adjust(left=0.3, bottom=0.3)
# Make a point slider to edit coordinates.
bleft = fig.add_axes([0, 2, 4,8])
bleft_slider = Slider(
ax=bleft,
label='bl',
valmin=1,
valmax=1080,
valinit=bl,
)
bright = fig.add_axes([0, 2, 4,8])
bright_slider = Slider(
ax=bright,
label='br',
valmin=1,
valmax=1080,
valinit=br,
)
tleft = fig.add_axes([0, 2, 4,8])
tleft_slider = Slider(
ax=tleft,
label="tl",
valmin=1,
valmax=1080,
valinit=tl,
orientation="vertical"
)
tright = fig.add_axes([0, 2, 4,8])
tright_slider = Slider(
ax=tright,
label="tr",
valmin=1,
valmax=1080,
valinit=tr,
orientation="vertical"
)
# The function to be called anytime a slider's value changes
def update(val):
pts.data(tleft_slider.val, bleft_slider.val, bright_slider.val, tright_slider.val)
fig.canvas.draw_idle()
# register the update function with each slider
bleft_slider.on_changed(update)
bright_slider.on_changed(update)
tleft_slider.on_changed(update)
tright_slider.on_changed(update)
plt.show()
all sliders should only move the points along the x-axis.
import numpy as np
from matplotlib.patches import Polygon
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
# Define initial parameters
bl = 200 #bottom left point
tl = 400 #top left point
tr = 600 #top right point
br = 1080 #bottom right point
# Create the figure and the line that we will manipulate
img = 'test1.jpg'
im = plt.imread(img)
fig, ax = plt.subplots()
im = ax.imshow(im, extent=[0, 1080, 0, 720])
pts = np.array([[bl,0], [tl,340], [tr,340], [br,0]])
p = Polygon(pts, closed=True, alpha=0.4, fc='green', ec="red")
ax = plt.gca()
ax.add_patch(p)
# adjust the main plot to make room for the sliders
fig.subplots_adjust(left=0.3, bottom=0.3)
# Make a point slider to edit coordinates.
bleft = fig.add_axes([0, 2, 4,8])
bleft_slider = Slider(
ax=bleft,
label='bl',
valmin=1,
valmax=1080,
valinit=bl,
)
bright = fig.add_axes([0, 2, 4,8])
bright_slider = Slider(
ax=bright,
label='br',
valmin=1,
valmax=1080,
valinit=br,
)
tleft = fig.add_axes([0, 2, 4,8])
tleft_slider = Slider(
ax=tleft,
label="tl",
valmin=1,
valmax=1080,
valinit=tl,
orientation="vertical"
)
tright = fig.add_axes([0, 2, 4,8])
tright_slider = Slider(
ax=tright,
label="tr",
valmin=1,
valmax=1080,
valinit=tr,
orientation="vertical"
)
# The function to be called anytime a slider's value changes
def update(val):
pts.data(tleft_slider.val, bleft_slider.val, bright_slider.val, tright_slider.val)
fig.canvas.draw_idle()
# register the update function with each slider
bleft_slider.on_changed(update)
bright_slider.on_changed(update)
tleft_slider.on_changed(update)
tright_slider.on_changed(update)
plt.show()