将交互式散景图从一个文件导出到另一个文件以用于面板

问题描述 投票:0回答:1

只是一个一般性问题,是否可以将我在一个文件中创建的散景图导出到另一个文件,该文件将用于使用面板的数据可视化仪表板(多个图)的样式和布局? 如果可以的话我希望仍然保持剧情的互动性。 作为参考,下面可能是示例代码片段/图的示例,我可能希望导出到另一个文件(此特定图的实际代码详细信息并不重要)

import pandas as pd
from math import pi
from bokeh.io import show
from bokeh.plotting import figure
from bokeh.models import CustomJS, Slider, HoverTool, ColumnDataSource
from bokeh.layouts import column
from bokeh.transform import cumsum
import colorcet

# Provide some data (is not the same as in your example):
data = {"Year": [1990, 1991, 1992],
        "Meningitis": [2159, 1592, 3914],
        "Alzheimer": [1116, 842, 534],
        "Parkinson": [371, 2376, 1675],
        "Malaria": [93, 189, 231]    
}
df = pd.DataFrame(data).set_index("Year")

df_transposed = df.transpose()
df

# Create one data frame per year and save them to a list of data frames.
# I find it easier to keep data frames at first, because operations can
# be applied at a higher level, so with less code:
year_dfs = []
for year in df_transposed.columns:
    year_df = df_transposed[year].to_frame(name="Deaths")
    year_df.index.name = "Disease"
    year_df["Percent"] = 100 * year_df["Deaths"]/year_df["Deaths"].sum()
    year_df["angle"] = year_df["Deaths"]/year_df["Deaths"].sum() * 2 * pi
    # Compute start and end angle for each disease, so that the first disease starts
    # at the top of the pie and the next ones appear in clockwise direction:
    year_df["start_angle"] = pi/2 - (year_df["angle"].cumsum() - year_df["angle"])
    year_df["end_angle"] = pi/2 - year_df["angle"].cumsum()
    # Set the color column. Feel free to use other palettes:
    year_df['color'] = list(map(lambda x: colorcet.b_glasbey_category10[x], range(len(year_df))))
    year_dfs.append(year_df)
year_df

# Convert the dataframes to dictionaries in the ColumnDataSource data format to hand over to Javascript:
year_dicts = []
for df in year_dfs:
    year_dicts.append(dict(ColumnDataSource(df).data))
year_dicts 

# Setup pie chart:
height = 550
width = 650
plot = figure(height=height, width=width, title="Diseases", x_range=(-0.6, 1.0))

plot_source = ColumnDataSource(year_dicts[0])

plot.wedge(x=0, y=1, radius=0.6, source=plot_source,
         start_angle="start_angle", end_angle="end_angle", direction="clock",
        line_color="white", legend_field='Disease', fill_color='color')
print(plot_source.data)

# Setup hover inspection:
hover = HoverTool()
hover.tooltips = [("Disease", "@{Disease}"),
                  ("Deaths", "@Deaths"),
                  ("Percent", "@Percent{f0.0} %")]
plot.add_tools(hover)

plot.axis.axis_label = None
plot.axis.visible = False
plot.grid.grid_line_color = None
plot.outline_line_color = None

plot.title.text_font_size = '16pt'

plot.toolbar.active_drag = None
plot.toolbar_location = None

# Setup slider and callback:
slider = Slider(start=1990, end=1992, value=1990, step=1, title="Select year")

callback = CustomJS(args=dict(plot_source=plot_source, sources=year_dicts), code="""
    const year_select = cb_obj.value
    const source_list = sources
    plot_source.data = source_list[year_select - 1990]
""")

slider.js_on_change('value', callback)

layout = column(slider, plot)

show(layout)

我尝试导出为 html,但不确定这是否可行,因为据我所知,html 脚本本身通常无法执行实时 python 代码

python data-science visualization bokeh panel
1个回答
0
投票

您可以pickle一个holoviews对象并将其加载到另一个系统上,如果这有帮助......

© www.soinside.com 2019 - 2024. All rights reserved.