Can you help me create a bar chart from this dataframe? I tried but nothing shown
@Mahmoud_Housam are you trying to create 5 different bar charts? One for every year? What error message are you getting?
@adamschroeder no error, but the chart is not created: frowning:
Would you help?
Sure. Give me about one hour. I don’t have access to a computer until then. Can you please copy-paste the code you used into this forum and share the .csv document with me.
I am trying to create one chart per year that is once the year is changed, the chart is edited reflecting numbers of this year
Ok. Can you share the code and csv document?
import dash
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objs as go
import pandas as pd
from dash.dependencies import Input, Output
df = pd.read_csv('redcap.csv')
app = dash.Dash()
year_options = []
for year in df['year'].unique():
year_options.append({'label': str(year), 'value': year})
app.layout = html.Div([
dcc.Graph(id='nursing titles'),
dcc.Dropdown(id='year_picker', options=year_options,
value=df['year'].min())
])
@app.callback(Output('nursing titles', 'figure'),
[Input('year_picker', 'value')])
def nursing_titles(year):
df = df[df['year'] == year]
traces = []
for year in df['year']:
count = df.groupby(['year'])['title'].value_counts()
x = count.index.astype(str).tolist()
y = count.loc[year, :].tolist()
data = go.Bar(x=x,
y=y,
text=count['title'].astype(str).tolist(),
textposition='auto')
layout = go.Layout(title='Titles 2020')
fig = go.Figure(data=data, layout=layout)
return fig
if __name__ == '__main__':
app.run_server()
@adamschroeder the csv file
You are asking the user to choose a year in the dropdown, and then you filter your df with that year
df = df[df['year'] == year]
And then you loop of the same year:
for year in df['year']:
This won’t work, because if user chooses 2020, for example, you are looping over 2020, and you “groupby” ONLY the year 2020, which is not appropriate.
Just change the defining function to this:
def nursing_titles(year):
dff = df[df['year'] == year]
count = dff['title'].value_counts()
data = go.Bar(x=count.index,
y=count.values)
layout = go.Layout(title='Titles 2020')
fig = go.Figure(data=data, layout=layout)
return fig
That should work.
would please take a look at my whole code not just the functions one? <3
really thanks adam
Hey,
I don’t think I changed anything outside of the function definition. And it works for me. Here’s the whole code.
import dash
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objs as go
import pandas as pd
from dash.dependencies import Input, Output
df = pd.read_csv('redcap.csv')
app = dash.Dash()
year_options = []
for year in df['year'].unique():
year_options.append({'label': str(year), 'value': year})
app.layout = html.Div([
dcc.Graph(id='nursing titles'),
dcc.Dropdown(id='year_picker', options=year_options,
value=df['year'].min())
])
@app.callback(Output('nursing titles', 'figure'),
[Input('year_picker', 'value')])
def nursing_titles(year):
dff = df[df['year'] == year]
count = dff['title'].value_counts()
data = go.Bar(x=count.index,
y=count.values)
layout = go.Layout(title='Titles 2020')
fig = go.Figure(data=data, layout=layout)
return fig
if __name__ == '__main__':
app.run_server(debug=True)
Please note that I used “debug=True” on the last line, this will help by telling you what might be wrong when you execute the script.