I have a problem, when creating a plot in which there is a polygon, using the sliders, I cannot change the coordinates of the polygon vertices relative to 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()
ax.plot(1000,1000)
# im = ax.imshow(im, extent=[0, 1080, 0, 720])
pts = np.array([[200,0], [400,340], [600,340], [1080,0]]) #
p = Polygon(pts, closed=True, alpha=0.4, fc='green', ec="black")
ax = plt.gca()
ax.add_patch(p)
# adjust the main plot to make room for the sliders
fig.subplots_adjust(bottom=0.4)
# Make a point slider to edit coordinates.
bleft = fig.add_axes([0.20, 0.27, 0.63, 0.05])
bleft_slider = Slider(
ax=bleft,
label='bottom-left',
valmin=1,
valmax=1080,
valinit=200,
)
tleft = fig.add_axes([0.20, 0.20, 0.63, 0.05])
tleft_slider = Slider(
ax=tleft,
label="top-left",
valmin=1,
valmax=1080,
valinit=342
)
tright = fig.add_axes([0.20, 0.13, 0.63, 0.05])# позиция слайдера (отступ слева, отступ снизу, ширина, длина)
tright_slider = Slider(
ax=tright,
label="top-right",
valmin=1,
valmax=1080,
valinit=600,
)
bright = plt.axes([0.20, 0.06, 0.63, 0.05])
bright_slider = Slider(
ax=bright,
label='bottom-right',
valmin=1,
valmax=1080,
valinit=1080,
closedmax=True
)
# The function to be called anytime a slider's value changes
def update(val):
xval = bright_slider.val
pts.set_xdata(xval)
plt.draw()
bright_slider.on_changed(update)
# 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()