python - Bokeh how to have an overlay histogram
问题描述
I made a histogram in bokeh and now I want to plot two histograms in the same graph (overlay histogram). How can I do that?
this is my code for one histogram:
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
import numpy as np
import pandas as pd
def generate_time_differences(n=1000, skew_p=0.1,mean=0,std=1,skew_mean=1,skew_std=6):
normal_dist = np.random.normal(loc=mean, scale=std, size=int(n * (1-skew_p)))
skewed_dist = np.random.normal(loc=skew_mean, scale=skew_std, size=int(n * skew_p))
return np.append(normal_dist, skewed_dist)
def generate_plot_data(data, density=True, bins=50):
hist, edges = np.histogram(data, density=density, bins=bins)
return pd.DataFrame({'top': hist,
'left': edges[:-1],
'right': edges[1:]})
data = generate_time_differences(n=1000, skew_p=0.1,mean=1000,std=100,skew_mean=2000,skew_std=500)
plot_data = generate_plot_data(data, density=True, bins=50)
# Create the blank plot
p = figure(plot_height = 300, plot_width = 600,
title = 'Test Histogram',
x_axis_label = 'Milliseconds',
y_axis_label = 'Frequency')
# Add a quad glyph
p.quad(bottom=0, top=plot_data['top'],
left=plot_data['left'], right=plot_data['right'],
fill_color='blue', line_color='blue', fill_alpha=0.5,line_alpha=0.5 )
# Show the plot
show(p)
解决方案
Bokeh just plots the glyphs you ask for, in the order you ask for. If you want to add a second histogram, make more calls to quad
with the new data.
推荐阅读
- powershell - Windows Powershell needs to print out information for a particular command regardless of whether it successfully executed or Not
- python - 删除数据框的元素(如果它们在列表中)
- c# - 有没有办法等到 oxyplot 中的情节完全刷新?
- flutter - 如何使用此按钮中的条件在颤动中显示警报对话框?
- java - 有人可以帮我找到 LeetCode 680 解决方案中的错误。有效回文 II - 一个著名的 Facebook 面试问题
- android-jetpack-compose - Android Jetpack Compose 列表使用重影图像重构
- c# - 子集模式实现
- spring - 尝试在 STS 中运行 gradle 项目
- sql - 我可以在 PostgreSQL 中跨表选择数据吗?
- python - 将值传递给下一个正向计算