Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用bokeh和dictionary创建饼图_Python_Dictionary_Bokeh - Fatal编程技术网

Python 使用bokeh和dictionary创建饼图

Python 使用bokeh和dictionary创建饼图,python,dictionary,bokeh,Python,Dictionary,Bokeh,所以我有这本字典: Counter({'United States': 157, 'United Kingdom': 93, 'Japan': 89, 'China': 63, 'Germany': 44, 'India': 42, 'Italy': 40, 'Australia': 35, 'Brazil': 32, 'France': 31, 'Taiwan': 31, 'Spain': 29, .....}) 我该如何在博基把它做成柱状图呢 我查过了,但真的没那么容易找到。因为我们在更

所以我有这本字典:

Counter({'United States': 157, 'United Kingdom': 93, 'Japan': 89, 'China': 63,
'Germany': 44, 'India': 42, 'Italy': 40, 'Australia': 35, 'Brazil': 32, 
'France': 31, 'Taiwan': 31, 'Spain': 29, .....})
我该如何在博基把它做成柱状图呢


我查过了,但真的没那么容易找到。

因为我们在更新版本的bokeh中没有bokeh图表,所以可以使用
wedge
创建饼图。您需要制作多个图示符才能获得图表。它还包括获得角度计算,以创建正确的楔块-

from collections import Counter
from bokeh.palettes import Category20c
x = Counter({'United States': 157, 'United Kingdom': 93, 'Japan': 89, 'China': 63,
'Germany': 44, 'India': 42, 'Italy': 40, 'Australia': 35, 'Brazil': 32, 
'France': 31, 'Taiwan': 31, 'Spain': 29})
x = pd.DataFrame.from_dict(dict(x), orient='index').reset_index().rename(index=str, \
                                                                         columns={0:'value', 'index':'country'})
x['val'] = x['value']
x['value'] = x['value']/x['value'].sum()*360.0
x['end']=x['value'].expanding(1).sum()
x['start'] = x['end'].shift(1)
x['start'][0]=0
r=[]
p = figure(plot_height=350, title="PieChart", toolbar_location=None, tools="")
for i in range(len(x)):
    r1 = p.wedge(x=0, y=1, radius=0.5,start_angle=x.iloc[i]['start'], end_angle=x.iloc[i]['end'],\
               start_angle_units='deg', end_angle_units = 'deg', fill_color=Category20c[20][i],\
                legend = x.iloc[i]['country'])
    Country = x.iloc[i]['country']
    Value = x.iloc[i]['val']
    hover = HoverTool(tooltips=[
        ("Country", "%s" %Country),
        ("Value", "%s" %Value)
    ], renderers=[r1])
    p.add_tools(hover)

p.xaxis.axis_label=None
p.yaxis.axis_label=None
p.yaxis.visible=False
p.xaxis.visible=False
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None

output_notebook()
show(p)

由于bokeh的较新版本中没有bokeh图表,您可以使用
wedge
创建饼图。您需要制作多个图示符才能获得图表。它还包括获得角度计算,以创建正确的楔块-

from collections import Counter
from bokeh.palettes import Category20c
x = Counter({'United States': 157, 'United Kingdom': 93, 'Japan': 89, 'China': 63,
'Germany': 44, 'India': 42, 'Italy': 40, 'Australia': 35, 'Brazil': 32, 
'France': 31, 'Taiwan': 31, 'Spain': 29})
x = pd.DataFrame.from_dict(dict(x), orient='index').reset_index().rename(index=str, \
                                                                         columns={0:'value', 'index':'country'})
x['val'] = x['value']
x['value'] = x['value']/x['value'].sum()*360.0
x['end']=x['value'].expanding(1).sum()
x['start'] = x['end'].shift(1)
x['start'][0]=0
r=[]
p = figure(plot_height=350, title="PieChart", toolbar_location=None, tools="")
for i in range(len(x)):
    r1 = p.wedge(x=0, y=1, radius=0.5,start_angle=x.iloc[i]['start'], end_angle=x.iloc[i]['end'],\
               start_angle_units='deg', end_angle_units = 'deg', fill_color=Category20c[20][i],\
                legend = x.iloc[i]['country'])
    Country = x.iloc[i]['country']
    Value = x.iloc[i]['val']
    hover = HoverTool(tooltips=[
        ("Country", "%s" %Country),
        ("Value", "%s" %Value)
    ], renderers=[r1])
    p.add_tools(hover)

p.xaxis.axis_label=None
p.yaxis.axis_label=None
p.yaxis.visible=False
p.xaxis.visible=False
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None

output_notebook()
show(p)

从Bokeh 0.13.0开始,这可以使用新的
cumsum
变换来实现:

from collections import Counter
from math import pi

import pandas as pd

from bokeh.palettes import Category20c
from bokeh.plotting import figure, show
from bokeh.transform import cumsum

x = Counter({
    'United States': 157,'United Kingdom': 93,'Japan': 89, 'China': 63,
    'Germany': 44,'India': 42, 'Italy': 40,'Australia': 35,'Brazil': 32,
    'France': 31,'Taiwan': 31,'Spain': 29
})

data = pd.DataFrame.from_dict(dict(x), orient='index').reset_index()
data = data.rename(index=str, columns={0:'value', 'index':'country'})
data['angle'] = data['value']/sum(x.values()) * 2*pi
data['color'] = Category20c[len(x)]

p = figure(plot_height=350, title="Pie Chart", toolbar_location=None,
           tools="hover", tooltips=[("Country", "@country"),("Value", "@value")])

p.wedge(x=0, y=1, radius=0.4, 
        start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
        line_color="white", fill_color='color', legend='country', source=data)

p.axis.axis_label=None
p.axis.visible=False
p.grid.grid_line_color = None

show(p)

从Bokeh 0.13.0开始,可以使用新的
cumsum
转换:

from collections import Counter
from math import pi

import pandas as pd

from bokeh.palettes import Category20c
from bokeh.plotting import figure, show
from bokeh.transform import cumsum

x = Counter({
    'United States': 157,'United Kingdom': 93,'Japan': 89, 'China': 63,
    'Germany': 44,'India': 42, 'Italy': 40,'Australia': 35,'Brazil': 32,
    'France': 31,'Taiwan': 31,'Spain': 29
})

data = pd.DataFrame.from_dict(dict(x), orient='index').reset_index()
data = data.rename(index=str, columns={0:'value', 'index':'country'})
data['angle'] = data['value']/sum(x.values()) * 2*pi
data['color'] = Category20c[len(x)]

p = figure(plot_height=350, title="Pie Chart", toolbar_location=None,
           tools="hover", tooltips=[("Country", "@country"),("Value", "@value")])

p.wedge(x=0, y=1, radius=0.4, 
        start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
        line_color="white", fill_color='color', legend='country', source=data)

p.axis.axis_label=None
p.axis.visible=False
p.grid.grid_line_color = None

show(p)

到目前为止,您尝试过什么?您尝试过简单地使用matplotlib吗?它是条形图还是饼图?@SimeonIkudabo不能使用matplotlib,我需要将条形图导出为HTML您尝试过什么?您尝试过简单地使用matplotlib吗?它是条形图还是饼图?@SimeonIkudabo不能使用matplotlib,我需要将酒吧聊天导出为HTMLTanks来回答这个问题,它促使我意识到这应该更容易做到,也可以相对简单地做到。我在一个单独的答案中添加了一个(略微)前瞻性的回答。感谢这个答案,它促使我意识到这应该更容易做到,也可以相对简单地做到。我在一个单独的答案中添加了一个(略微)前瞻性的回答。