Hello all -
I’m sure I’m trying to fit a square peg into a round hole, but I was hoping to get some direction here as I can’t seem to find an efficient way to solve a problem I am encountering.
I have created a simple bar graph using a dataframe that I have generated. This dataframe is created in part by using numpy.random.randint(). What I would like to be able to do is add an update button that lets a user randomize the dataframe and then display that new chart. I obviously don’t want to create a large amount of traces (as well as a large boolean list to instruct which traces to show) as that would be resource intensive.
Essentially, my dataframe has three columns, created here:
id = [] #creating the id column values
for i in range(0,40):
id.append(i+1)
flowers=[] #creating the flower column values
for i in range(0,40):
if i<10:
val='purple'
elif i<20:
val='red'
elif i<30:
val='yellow'
else:
val='orange'
flowers.append(val)
data = {'id':id,'flower':flowers,'count':1} #creating the data dictionary to be read into a dataframe. added count column for graphing purposes
df = pd.DataFrame(data=data) #creating the df dataframe
My chart code is here:
colors=[] #creating empty color list to populate with color values
df_rand=df.sample(n=40, random_state=np.random.randint(100000000)) #creating new dataframe based on randomized full sample from df dataframe
df_rand.reset_index(inplace=True) #resetting the index so the graph reads in the new order correctly
for i in range(0,40): #creating the logic for assigning the correct color to flower value
colors.append(df_rand['flower'][i])
fig = go.Figure(data=[go.Bar(
x=df['id'],
y=df['count'],
marker_color=colors
)]) #creating bar chart using df[id] and df[count] and color list
fig.update_yaxes(range=[0, 5]) #resizing y axis
fig.show()
Any thoughts?