Reputation: 41
I have a dictionary of dates (keys) to a value for each date. I'm trying to show this in a simple bar graph using plotly-express in python. I've tried putting it in a pandas DataFrame and Series object and just using a plain dict, but I seem to get an error/not what I want each time when I try to put it in a plotly express bar graph as such:
fig = px.bar(daily_charge_dict, x=daily_charge_dict.keys(),y=daily_charge_dict.values(), barmode="group")
Any suggestions on how to complete this? Thanks!
Upvotes: 4
Views: 13081
Reputation: 1327
To plot a bar from a dictionary, the x and y must be a list. For example in your case, you want x axis to be a list of dates and y axis to be some values for each date. So the dictionary should look like:
the_dict = {'dates': ['2020-01-01', '2020-01-02'], 'y_vals': [100,200]}
So rather than have several keys of dates, have just a two key dictionary, with the list of dates being the first element and the list of corresponding values being the second.
Then plot them using plotly express as :
import plotly.express as px
fig = px.bar(the_dict, x='dates', y='y_vals')
fig.show()
Upvotes: 4