0

I have done basically the same plot before with different data, I don't exactly know why but this one insists in fail me once I insert the x_range into it. This code below works just fine, although I don't have all the years on the xaxis.

dfnt = dfn[['Total Transactions', 'Total Non-Occupiers']]
xr = str(dfnt.index.values)
xl = ['Total Transactions', 'Total Non-Occupiers']
ld = {'2010':xl, '2011':xl, '2012':xl, '2013':xl, '2014':xl, '2015':xl, '2016':xl, '2017':xl, '2018':xl}
rowX = ['2010', '2011','2012','2013','2014','2015','2016', '2017', '2018']
#x1 = [(y, t) for y in rowX for t in xl]


sourcent = ColumnDataSource(data=dict( x = list(dfnt.index.values),
                                    y=dfnt['Total Transactions'],
                                    y1=dfnt['Total Non-Occupiers']))
pn = figure(plot_height=350, plot_width=550, title='Properties Transactions', y_axis_label=None, x_axis_label=None, tools = 'pan, wheel_zoom, box_zoom, reset')
pn.vbar(x=dodge('x', 0.0), top='y', width=0.3, source=sourcent, color='#440154', legend=value('Total Transactions'))
pn.vbar(x=dodge('x', -0.35), top='y1', width=0.3, source=sourcent, color='#FDE724', legend=value('Total Non-Occupiers'))
pn.legend.location = 'top_left'
hoverpn = HoverTool()
hoverpn.tooltips=[('Transactions', 'overall @y / non-occupiers @y1')]
pn.add_tools(hoverpn)
tick_labelspn = {'10000':'10K','20000':'20K','30000':'30K','40000':'40K','50000':'50K', '60000':'60K'}
pn.yaxis.major_label_overrides = tick_labelspn
pn.legend.background_fill_alpha=None
pn.legend.border_line_alpha=0
pn.legend.label_text_font_size = "11px"
pn.y_range.end = dfnt.values.max()*1.1+1
pn.legend.click_policy="hide"
pn.title.text_font_size = '15px'
pn.xaxis.major_label_text_font_style = 'bold'
pn.grid.grid_line_color=None
pn.toolbar.autohide = True
show(pn)

without x_range

Once I add the x_range into it the bars will disappear.

pn = figure(x_range=FactorRange(*ld),plot_height=350, plot_width=550, title='Properties Transactions', y_axis_label=None, x_axis_label=None, tools = 'pan, wheel_zoom, box_zoom, reset')

The dataset or dfnt is below in case: dfnt

ReinholdN
  • 526
  • 5
  • 22
  • Are the years in the data set numbers or strings? – bigreddot Feb 07 '20 at 15:57
  • @bigreddot string – ReinholdN Feb 07 '20 at 16:26
  • @bigreddot that was a tuple, I did the follow: ```xstr = ' '.join(map(str, rowX))``` Instead a got an error: ERROR:bokeh.core.validation.check:E-1019 (DUPLICATE_FACTORS): FactorRange must specicy a unique list of categorical factors for an axis: duplicate factors found: '2', '0', '1', ' ' – ReinholdN Feb 07 '20 at 17:06
  • 1
    I'm not really following. What was a tuple? That message looks like you have given a list of individual characters for the factors or a factor range. It should be a list of the (string) years, and the data itself (in the CDS) should also be (string) years. – bigreddot Feb 07 '20 at 17:15
  • @bigreddot sorry the confusion! I tried to add just the years as string as shown above: ```xstr = ' '.join(map(str, rowX)``` which gave me that 1019 error. Before I used a dictionary as I have been using for a good while with bokeh, such as the below: ```xl = ['Total Transactions', 'Total Non-Occupiers'] ld = {'2010':xl, '2011':xl, '2012':xl, '2013':xl, '2014':xl, '2015':xl, '2016':xl, '2017':xl, '2018':xl}``` then added it into the figure: – ReinholdN Feb 07 '20 at 17:19
  • ```pn = figure(x_range=FactorRange(*ld),plot_height=350, plot_width=550, title='Properties Transactions', y_axis_label=None, x_axis_label=None, tools = 'pan, wheel_zoom, box_zoom, reset') ``` Which gave me an empty canvas – ReinholdN Feb 07 '20 at 17:19
  • @bigreddot thanks man. I fixed it, I got confused. That was simpler than I thought. – ReinholdN Feb 07 '20 at 21:56
  • Glad to hear! Can you self-answer and accept? – bigreddot Feb 08 '20 at 07:07

1 Answers1

0

Thanks @bigreddot, the guy seems to be the Bokeh's guru in this community. Anyways keep a string format. I was using the df.index in the CDS as x, it has to be a string variable declared outside the CDS, that will be used in both locations(CDS/x_range) as below:

rowX = '2010', '2011','2012','2013','2014','2015','2016', '2017', '2018'
sourcent = ColumnDataSource(data=dict( x = rowX,
                                    y=dfnt['Total Transactions'],
                                    y1=dfnt['Total Non-Occupiers']))
pn = figure(x_range=rowX, plot_height=350, plot_width=550, title='Properties Transactions in Ireland', y_axis_label=None, x_axis_label=None, tools = 'pan, wheel_zoom, box_zoom, reset')

Which will work just fine.

ReinholdN
  • 526
  • 5
  • 22