'Drop down menu for the graph

I am new to python and I'm trying to make a drop down menu for this graph that shows temperatures at different times of day.The datas are imported from a csv file . Below is the code:

import csv

import matplotlib.pyplot as plt 
 
x=[]
 
y=[] 

z=[]
 
w=[] 

class grafice_statice():

    def run(self):
        with open('temperatura.csv', 'r') as csvfile:
            date = csv.reader(csvfile, delimiter=',')
            for row in date:
                try:
                    y.append(float(row[0]))
                    z.append(float(row[1]))
                    w.append(float(row[2]))
                    x.append(row[3])
                except Exception as e:
                    pass

        
        plt.figure(1)
        plt.plot(x,z, color='g', linestyle='dotted', marker='o', label='Temp 2(°C)!')
        plt.plot(x,y, color='m', linestyle='solid', marker='P', label='Temp 1(°C)!')
        plt.plot(x,w, color='r', linestyle='dashdot', marker='D', label='Temp 3(°C)!')

        plt.xlabel('Timpul')
        plt.ylabel('Temperatura(°C)')
        plt.title('Temperatura in functie de timp', fontsize = 18)
        plt.legend()

        plt.xticks([0,50,99])
        plt.ylim((-5,5))
        plt.show()
             
    grafice_stat=grafice_statice()
    grafice_stat.run()

How can I do this?



Solution 1:[1]

If I understand your question correctly, you are trying to set up a dropdown menu to let the user choose which temperature plot to display. One way to do this is to use the ipywidgets Dropdown function. More info about it can be found here.

You can find an example below, where I created 3 random temperature vectors (since I didn't have access to your data). They are stored in the list temp. I then let the user choose which temperature vector to plot through the dropdown menu with the line dropdown=widgets.Dropdown(options=[('Temp1', 0), ('Temp2', 1), ('Temp3', 2)],value=0,description='Temp'). Once the user chooses, it automatically update the figure through the on_change(change) function.

So, overall the code looks like that:

import ipywidgets as widgets
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display, clear_output

def t_temp(N_points):
  temp_min=-20
  temp_max=40
  temp=np.random.choice(np.arange(temp_min,temp_max),size=N_points)
  return temp 

colors=['tab:blue','tab:orange','tab:green']
N_points=100
t=1+np.arange(N_points)
N_temp_types=3
temp=[]
[temp.append(t_temp(N_points)) for i in range(N_temp_types)]

dropdown=widgets.Dropdown(options=[('Temp1', 0), ('Temp2', 1), ('Temp3', 2)],value=0,description='Temp')
plt.plot(t,temp[int(dropdown.value)],lw=2,color=colors[int(dropdown.value)],label='Temp '+ str(int(dropdown.value)+1))
plt.legend()

def on_change(change):
    if change['type'] == 'change' and change['name'] == 'value':
        clear_output()
        display(dropdown)
        plt.plot(t,temp[int(dropdown.value)],lw=2,color=colors[int(dropdown.value)],label='Temp '+ str(int(dropdown.value)+1))
        plt.legend()
dropdown.observe(on_change)

display(dropdown)

And the output gives:

When "Temp1" is selected:

enter image description here

When "Temp2" is selected:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1