我正在使用Bokeh生成一些图表,试图想象一下调查反馈。我正在使用堆叠的Bar高级对象,我有两个问题。
我写的只是POC,因此代码包含自己的测试数据。我试过了:
legend_sort_direction = 'ascending'
,但它不会对图例进行排序(这是一个bug错误吗?)代码:
from bokeh.charts import Bar, output_file, show
from bokeh.layouts import row, column
from bokeh.io import output_notebook
from random import randrange
import random
from collections import OrderedDict
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import HoverTool
from bokeh.models.renderers import GlyphRenderer
from bokeh.charts.attributes import CatAttr
from bokeh.models import Range1d
import calendar
ChapterName = ['05: Information security policies',
'06: Organization of information security',
'07: Human resource security',
'08: Asset management',
'09: Access control',
'10: Cryptography',
'11: Physical and environmental security',
'12: Operations security',
'13: Communications security',
'14: System acquisition, development and maintenance',
'15: Supplier relationships',
'16: Information security incident management',
'17: Information security aspects of business continuity management',
'18: Compliance']
# Do some random stuff here
UserName = ['Vanita.Marton',
'Tula.Falls',
'Simonne.Linzy',
'Donald.Eggebrecht',
'Deadra.Jahn',
'Leah.Conrad',
'Susie.Dolin',
'Gretta.Brumbelow',
'Dong.Mowrey',
'Georgetta.Delk',
'Princess.Mccook',
'Joe.Edgley',
'Lonnie.Oppenheimer',
'Ethel.Presler',
'Lura.Byerley',
'Maile.Mccaskey',
'Charissa.Degroat',
'Deloras.Deskin',
'Travis.Phillis',
'Rachelle.Bane']
# do some random stuff here
Brands = ['AB Flug',
'AC',
'Acura',
'Aerogear',
'APR Performance',
'Aston Martin',
'Audi',
'Bentley',
'BMW',
'Border',
'Buick',
'Cadillac',
'Chevrolet',
'Chrysler',
'Datsun',
'Dodge',
'Eagle',
'Ferrari',
'Ford',
'Guldstrand',
'Hennessey',
'Honda',
'Hyundai',
'Infiniti',
'INGS',
'Jaguar',
'Koenigsegg',
'Lancia',
'Lexus',
'Lingenfelter',
'Lotus',
'Mazda',
'McLaren',
'Mercedes',
'Mine',
'MINI',
'Mitsubishi',
'Mugen',
'Nissan',
'Opel',
'Pagani',
'Panoz',
'Peugeot',
'Pontiac',
'Porsche',
'Renault',
'Saab',
'Saleen',
'Seat',
'Shelby',
'Sparco',
'Subaru',
'Tommy Kaira',
'Tom',
'Toyota',
'TVR',
'Vauxhall',
'Veilside',
'VIS Racing',
'Volkswagen',
'Volvo',
'Wings West']
Comment = ['Just a comment','Another comment']
Answers = ['G','R','P','B']
g_data = r_data = p_data = b_data = data = {
'UserName': [],
'DeclName' : [],
'Quid' : [],
'ChapterName' : [],
'AnswerCodes' : [],
'Comment' : [],
}
for i in range(1, 10000):
currentUserName = random.choice(UserName)
currentDeclName = random.choice(Brands) # Let car names be declaration names
currentQuid = randrange(1,100)
currentAnswer = random.choice(Answers)
currentChapterName = random.choice(ChapterName)
currentComment = random.choice(Comment)
# Data structure with all answers
data['UserName'].append(currentUserName)
data['DeclName'].append(currentDeclName)
data['Quid'].append(currentQuid)
data['ChapterName'].append(currentChapterName)
data['AnswerCodes'].append(currentAnswer)
data['Comment'].append(currentComment)
if(currentAnswer =='G'): # Reduced data structure with the G answers
g_data['UserName'].append(currentUserName)
g_data['DeclName'].append(currentDeclName)
g_data['Quid'].append(currentQuid)
g_data['ChapterName'].append(currentChapterName)
g_data['AnswerCodes'].append(currentAnswer)
g_data['Comment'].append(currentComment)
bar = Bar(data, values='AnswerCodes', label=CatAttr(columns=['UserName'], sort=True), agg='count', legend_sort_direction = 'ascending', stack='AnswerCodes', title="Answer Codes per user", plot_width=900, legend='top_right', tools='pan,wheel_zoom,box_zoom,save,undo, redo, reset')
g_bar = Bar(g_data, values='AnswerCodes', label='UserName', agg='count', legend_sort_direction = 'ascending', stack=CatAttr(columns=['ChapterName'], sort=True), title="Compliant per chapter", plot_width=900, legend='top_right', tools='hover')
glyph_renderers = bar.select(dict(type=GlyphRenderer))
bar_source = glyph_renderers[0].data_source
hover = bar.select(dict(type=HoverTool))
hover.tooltips = [
('Answer code', '@AnswerCodes'),
('User', '@UserName')
]
hover = g_bar.select(dict(type=HoverTool))
hover.tooltips = [
('Chapter', '@ChapterName'),
('User', '@UserName')
]
show(column(bar, g_bar))