You can create a line graph and add a line to it after pushing a button in Python using the matplotlib library. Here is an example of how you can achieve this:
main.py389 chars17 lines
In this example, we first define a function add_line
that will be called when the button is clicked. Inside this function, we define the x and y values for the new line to be added and use the plt.plot()
function to add the line to the graph.
We then create the figure and axes using plt.subplots()
, adjust the position of the axes so that the button does not overlap with the graph using plt.subplots_adjust()
, and create the button using plt.Button()
. We specify the position of the button using the plt.axes()
function.
Finally, we call button.on_clicked()
to connect the button to the add_line
function and display the graph using plt.show()
.
When you run this code, a graph will be displayed with a button labeled "Add Line". When you click the button, it will add a red dashed line to the graph.
Note: This code assumes that you have matplotlib installed. You can install it using pip install matplotlib
.
gistlibby LogSnag