Я могу создать одну гистограмму в моем flask WebApp, используя этот учебник: https://www.fullstackpython.com/blog/responsive-bar-charts-bokeh-flask-python-3.html
Я бы хотел видеть несколько гистограмм на та же страница HTML, но не одна.
Я пробовал следующее:
@app.route("/<string:quiz_name>")
def chart(quiz_name):
quizQuestions = Metrics.retrieveQuestionsByQuizName(quiz_name) #Returns Json with a list of questions
allQuestions = quizQuestions["questions"] #stores the list of questions
data = {"choices": [], "responseCount": []} #this is the data I want to plot with Bokeh
plots = [] #will store all of the plots -- one plot represents one bar chart
if not (allQuestions is None):
for question in allQuestions:
questionChoicesList = question["choices"]
responsesList = question["responses"]
for i in range(0, len(questionChoicesList)):
data['choices'].append(questionChoicesList[i])
responseCount = aggregateResponseCountForAnswerChoice(questionChoicesList[i], responsesList) #This returns the correct counts to be graphed using Bokeh
data['responseCount'].append(responseCount)
data["choices"] = questionChoicesList #will be the X-axis labels
hover = create_hover_tool()
questionPrompt = question['prompt']
plot = create_bar_chart(data, questionPrompt, "choices",
"responseCount", hover) #here is where the plot (representing the bar chart and its data) are created
script, div = components(plot)
plots.append(plot)
print('plots is of size: {}'.format(len(plots)))
script, div = components(plots)
return render_template("chart.html", quiz_name=quiz_name,
the_div=div, the_script=script)
def create_bar_chart(data, title, x_name, y_name, hover_tool=None,
width=1200, height=300):
"""Creates a bar chart plot with the exact styling for the centcom
dashboard. Pass in data as a dictionary, desired plot title,
name of x axis, y axis and the hover tool HTML.
"""
source = ColumnDataSource(data)
xdr = FactorRange(factors=data[x_name])
ydr = Range1d(start=0,end=max(data[y_name])*1.5)
tools = []
if hover_tool:
tools = [hover_tool,]
plot = figure(title=title, x_range=xdr, y_range=ydr, plot_width=width,
plot_height=height, h_symmetry=False, v_symmetry=False,
min_border=0, toolbar_location="above", tools=tools,
responsive=True, outline_line_color="#666666")
glyph = VBar(x=x_name, top=y_name, bottom=0, width=.8,
fill_color="#e12127")
plot.add_glyph(source, glyph)
xaxis = LinearAxis()
yaxis = LinearAxis()
plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker))
plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker))
plot.toolbar.logo = None
plot.min_border_top = 0
plot.xgrid.grid_line_color = None
plot.ygrid.grid_line_color = "#999999"
plot.yaxis.axis_label = "Student Answer Counts"
plot.ygrid.grid_line_alpha = 0.1
plot.xaxis.axis_label = "Question Answer Choices"
plot.xaxis.major_label_orientation = 1
return plot
Гистограмма отображается очень хорошо, когда я отображаю только одну гистограмму. Количество и метки идеально соответствуют данным. Но когда я добавляю вторую диаграмму, отсчеты уже не точны. Как правильно добавить второй, третий, четвертый график?