散景中的分类条形图

时间:2019-05-06 10:08:02

标签: python pandas bokeh

我有一个数据框

    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

在这种情况下,我想体验x轴和Microsft,SAS,Salesforce y轴

到目前为止我尝试过的事情:

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 = {}

对于范围内的我(len(full)):     数据[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()

它仅显示索引的最后一个值,即salesforce ..我想显示所有值;

1 个答案:

答案 0 :(得分:1)

代码中还有一些小错误,因此在笔记本电脑上什么也没显示。下面的代码应该可以工作并提供所需的输出。

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