Bokeh是Python中的数据可视化库, 可提供高性能的交互式图表和绘图。Bokeh输出可以通过笔记本, html和服务器等各种介质获得。可以将bokeh图嵌入Django和flask应用程序中。
Bokeh为用户提供了两个可视化界面:
bokeh.models:一种低级接口, 为应用程序开发人员提供了高度的灵活性。
bokeh.plotting:用于创建可视字形的高级界面。
要安装bokeh软件包, 请在终端中运行以下命令:
pip install bokeh
用于生成散景图的数据集是从Kaggle(https://www.kaggle.com/mcdonalds/nutrition-facts)中收集的。
代码1:散点标记
要创建散点圆标记,使用circle()方法。
# import modules
from bokeh.plotting import figure, output_notebook, show
# output to notebook
output_notebook()
# create figure
p = figure(plot_width = 400 , plot_height = 400 )
# add a circle renderer with
# size, color and alpha
p.circle([ 1 , 2 , 3 , 4 , 5 ], [ 4 , 7 , 1 , 6 , 3 ], size = 10 , color = "navy" , alpha = 0.5 )
# show the results
show(p)
输出:
代码2:单线
要创建单线, 使用line()方法。
# import modules
from bokeh.plotting import figure, output_notebook, show
# output to notebook
output_notebook()
# create figure
p = figure(plot_width = 400 , plot_height = 400 )
# add a line renderer
p.line([ 1 , 2 , 3 , 4 , 5 ], [ 3 , 1 , 2 , 6 , 5 ], line_width = 2 , color = "green" )
# show the results
show(p)
输出:
代码3:条形图
条形图显示带有矩形条的分类数据。条的长度与所表示的值成比例。
# import necessary modules
import pandas as pd
from bokeh.charts import Bar, output_notebook, show
# output to notebook
output_notebook()
# read data in dataframe
df = pd.read_csv(r "D:/kaggle/mcdonald/menu.csv" )
# create bar
p = Bar(df, "Category" , values = "Calories" , title = "Total Calories by Category" , legend = "top_right" )
# show the results
show(p)
输出:
代码4:箱形图
箱形图用于表示图上的统计数据。它有助于总结数据中存在的各种数据组的统计属性。
# import necessary modules
from bokeh.charts import BoxPlot, output_notebook, show
import pandas as pd
# output to notebook
output_notebook()
# read data in dataframe
df = pd.read_csv(r "D:/kaggle /mcdonald /menu.csv" )
# create bar
p = BoxPlot(df, values = "Protein" , label = "Category" , color = "yellow" , title = "Protein Summary (grouped by category)" , legend = "top_right" )
# show the results
show(p)
输出:
代码5:直方图
直方图用于表示数值数据的分布。直方图中矩形的高度与分类间隔中值的频率成正比。
# import necessary modules
from bokeh.charts import Histogram, output_notebook, show
import pandas as pd
# output to notebook
output_notebook()
# read data in dataframe
df = pd.read_csv(r "D:/kaggle /mcdonald /menu.csv" )
# create histogram
p = Histogram(df, values = "Total Fat" , title = "Total Fat Distribution" , color = "navy" )
# show the results
show(p)
输出:
代码6:散点图
散点图用于绘制数据集中两个变量的值。它有助于找到所选的两个变量之间的相关性。
# import necessary modules
from bokeh.charts import Scatter, output_notebook, show
import pandas as pd
# output to notebook
output_notebook()
# read data in dataframe
df = pd.read_csv(r "D:/kaggle /mcdonald /menu.csv" )
# create scatter plot
p = Scatter(df, x = "Carbohydrates" , y = "Saturated Fat" , title = "Saturated Fat vs Carbohydrates" , xlabel = "Carbohydrates" , ylabel = "Saturated Fat" , color = "orange" )
# show the results
show(p)
输出:
参考文献:
https://bokeh.pydata.org/en/latest/
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
评论前必须登录!
注册