Problem
I want to move a single graph in a stack plot with the mouse and update the plot instantly. The values shouldn't only change in the plot (graphical), the list with the values should also be updated, so that I can print them in a file.
Question
Is it possible to implement this in python with matplotlib or would you recommend me something else? The implementation need to be in python. Matplotlib was recommended to me, but if it's not possible with this package or there is a better one please leave a comment. When you have a code example feel free to share it.
Example
The x-axis represent time (from today to the future) and the y-axis an anmount of resources. It isn't possible to move a graph to the past (left) when a value would be cut of that is != 0. You can move any graph to the future (right), nothing get's cut. for example I can't move "O" to the left, but I can move "R" and "Y" (only one time) to the left.
In the example I used lists with only 6 entries, but you could imagine that they are always long enought.
Values:
x-ax = [0, 1, 2, 3, 4, 5]
y-ax = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
R = [0, 1, 2, 1, 1, 1]
Y = [0, 2, 1, 2, 1, 0]
O = [5, 2, 1, 1, 1, 1]
after moving Y one right
R = [0, 1, 2, 1, 1, 1]
Y = [0, 0, 2, 1, 2, 1]
O = [5, 2, 1, 1, 1, 1]
What I have
I define a figure and add a subplot, now I use the stackplot function to plot my data.
fig = plt.figure()
ax1 = fig.add_subplot(111)
stackPlots = ax1.stackplot(x-ax, R, Y, O)
Thanks to "ImportanceOfBeingErnest"! I added 3 Events and connected them
cid_press = fig.canvas.mpl_connect('button_press_event', on_press)
cid_release = fig.canvas.mpl_connect('button_release_event', on_release)
cid_move = fig.canvas.mpl_connect('motion_notify_event', motion_notify)
def on_press(event):
if(event.button == 1):
for stack_p in stackPlots:
contains, attrd = stack_p.contains(event)
if contains:
selected_stack = stack_p
break
if not contains:
return
# I can change the color, but how can I move it?
selected_stack .set_facecolors('black')
plt.draw()
print("clicked on", attrd, selected_stack)
#print('you pressed', event.button, event.xdata, event.ydata)
def on_release(event):
if (event.button == 1):
print('you released', event.button, event.xdata, event.ydata)
def motion_notify(event):
if (event.button == 1):
return
print('you moved', event.button, event.xdata, event.ydata)
When I click on a Graph now it changes it's color to black. So I know I've got the right one, but I want to move it. The "stackplot" Method returns a List of "PolyCollection". I think that I should redo the whole plot, but I need to find out which "PolyCollection" correspond with which array (R, Y, O)...
Thanks for your help!