nishtha vijay
nishtha vijay

Reputation: 21

Categorical bar chart in bokeh

I have a dataframe

    Experience  Microsoft  SAS  Salesforce
0     1 To 3          38   29          48
1   12 To 18           4    2           4
2     3 To 8          46   22          45
3    8 To 12          12    6           8

In this I want Experience in x axis and Microsft,SAS,Salesforce in y axis

what I tried till now:

table2 = pd.read_csv('Experience.csv')

full=table2.columns.tolist() full_val=table2.values.T.tolist()

tech=table2.drop(['Experience'] , axis=1,) he=tech.max()

data = {}

for i in range(len(full)): data[full[i]] = full_val[i]

source = ColumnDataSource(data=dict(x=table2['Experience'],
                                    y=value1))


p = figure(x_range=exp, y_range=(0, 50), plot_height=350, title="Experience wise opening",
           toolbar_location=None, tools="")


p.vbar(x=dodge('x', -0.25, range=p.x_range), top='y', width=0.2, source=source,
            legend=value(key))


p.show()

It shows only the last value of index that is salesforce .. I want to show all the values;

Upvotes: 0

Views: 1364

Answers (1)

Jasper
Jasper

Reputation: 1795

There were some more small mistakes in the code so it didn't show anything on my laptop. The code below should work and give the desired output.

from bokeh.models import ColumnDataSource, LabelSet
from bokeh.plotting import figure
from bokeh.transform import dodge
from bokeh.io import show
from bokeh.palettes import Viridis

data = {'Experience': ['1 To 3', '12 To 18', '3 To 8', '8 To 12'], 'Microsoft': [38, 4, 46, 12], 'SAS': [29, 2, 22, 6], 'Salesforce': [48, 4, 45, 8]}
source = ColumnDataSource(data)
exp = data['Experience']
ys = list(data.keys())
ys.remove('Experience')
TOOLTIPS = [("Experience", "@Experience")]
p = figure(x_range=exp, y_range=(0, 60), plot_height=350, title="Experience wise opening", tooltips=TOOLTIPS)
colorList = Viridis[len(ys)]
labels = []
for y, offset, color in zip(ys, [-0.25, 0, 0.25], colorList):
    labels = LabelSet(x=dodge('Experience', offset, range=p.x_range), y=y, text=y, source=source, text_align='center')
    p.vbar(x=dodge('Experience', offset, range=p.x_range), top=y, width=0.2, source=source, legend=y + ' ', color=color)
    p.add_layout(labels)
    TOOLTIPS.append((y, "@"+y))
p.legend.click_policy="hide"
show(p)

enter image description here

Upvotes: 1

Related Questions