简体   繁体   中英

Bokeh vbar figure with reversed y-axis

I'm trying to create a vbar figure in Bokeh 1.1 for survey ratings data that follow the reverse order that one would normally expect. In this data set "Excellent" is a 1.0 (the highest score) and "Extremely Poor" is 5.0 (the lowest score) and I am plotting the average values. Hence, I'd like to have a bar that goes from 5.0 at the bottom of the chart up to the top of 1.25, or whatever the average value is.

In the code below I specified:

y_range = (5,1) 

which puts the axis in the right direction but also unfortunately (and as expected) flips the vbar so it comes down from the top.

In another place I used:

fig.y_range.flipped = True 

as an ad hoc solution for a similar challenge with a line figure, but when applied to a vbar this still produces bars that come down from the top of the chart, whereas I still want the bar to come up from the bottom.

Here's a quick version of the code I've been using:

import pandas as pd
import numpy as np
from bokeh.plotting import figure
from bokeh.io import show, output_file, output_notebook
from bokeh.transform import dodge

Years = [2010, 2011, 2010, 2012, 2011, 2013, 2013]
ratings_df = pd.DataFrame(Years)
ratings_df.columns = ['Years']
ratings_df['Math_Mean_Ratings'] = [1.0, 2.0, 1.5, 3.0, 5.0, 1.0, 2.0]
ratings_df['German_Mean_Ratings'] = [3.0, 1.0, 2.0, 3.0, 4.0, 1.5, 2.0]
ratings_df

table1 = pd.pivot_table(ratings_df, values=['Math_Mean_Ratings', 'German_Mean_Ratings'], index='Years', aggfunc=np.mean)

fig_title = 'Instructor Ratings: 2010 to 2013'
fig = figure(plot_width=600, plot_height=300, title = fig_title, y_range = (5,1))

fig.vbar(x=dodge('Years', -0.2, range=p2.x_range), width = 0.3, top='Math_Mean_Ratings', source=table1, color='deepskyblue')
fig.vbar(x=dodge('Years', 0.2, range=p2.x_range), width = 0.3, top='German_Mean_Ratings', source=table1, color='midnightblue')

show(fig)

The ultimate output will have a lot of years and a lot of different averages for each year and I'll have to frequently update it, so I'd rather use a mid-level plotting interface like vbar than create independent rectangles for all the values.

Bokeh vbar has bottom=0 as the default so if you flip the axis, and leave that default, then it only makes sense for the bar to flip as well. Presumably you would want to set bottom=6 (or whatever makes sense as the "lowest" value on your scale).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM