【问题标题】:Empty Bokeh Plot空散景图
【发布时间】:2021-03-17 16:29:18
【问题描述】:

我正在尝试使用散景从我的 DataFrame 中绘制数据,并且绘图始终为空。下面是我的函数。

def statplot(stats_df,plotname):
    try:
        source_df = ColumnDataSource(data=stats_df.sort_values(by=['log_time']))
        print(source_df.data)

        output_file(plotname,'General Statistics')

        datetime_tick_formats = {
                key: ["%a %b %d %H:%M:%S"]
                for key in ("seconds", "minsec", "minutes", "hourmin", "hours", "days")}

        hover1 = HoverTool(tooltips=[('Date','@log_time{%Y-%m-%d %H:%M:%S}'),('Value','@PrivateMem')],formatters = {'@log_time':'datetime'})

        p1 = figure(title="PrivateMemory MB",plot_width=800, plot_height=400,x_axis_type="datetime")
        p1.xaxis.axis_label="Time"
        p1.yaxis.axis_label="MB"
        p1.xaxis.formatter = DatetimeTickFormatter(**datetime_tick_formats)
        p1.line(x='log_time',y='PrivateMem',source=source_df,line_width=2,color="red",legend_label='Private Memory')

        p1.add_tools(hover1)

        plot = gridplot([p1],ncols=1)
        save(plot)

我有打印 source.data 来验证我的 ColumnDataSource 是否有任何值,并且看起来确实如此。以下是样本数据

{'index': array([ 550,  551,  552, ..., 1658, 1659, 1660]), 'log_time': array(['2021-02-19T17:08:27.000000000', '2021-02-19T17:10:59.000000000',
       '2021-02-19T17:11:59.000000000', ...,
       '2021-02-24T08:33:38.000000000', '2021-02-24T08:34:38.000000000',
       '2021-02-24T08:35:38.000000000'], dtype='datetime64[ns]'), 'PrivateMem': array([' 33', ' 67', ' 72', ..., ' 91', ' 90', ' 90'], dtype=object), 'PagedMem': array([' 33', ' 67', ' 72', ..., ' 91', ' 90', ' 90'], dtype=object), 'ThreadCount': array([' 36', ' 54', ' 49', ..., ' 44', ' 45', ' 45'], dtype=object), 'HandleCnt': array([' 1134', ' 2214', ' 2232', ..., ' 2498', ' 2488', ' 2498'],
      dtype=object)}

任何想法我在阻止绘制值的函数中做错了什么?

【问题讨论】:

  • 一目了然。浏览器的 JavaScript 控制台中是否有任何消息?
  • @bigreddot 我在控制台中看到一条消息“散景无法设置初始范围”
  • 我还将 log_time 列转换为 datetime。所以不知道为什么这会失败。 stats_df['log_time'] = pd.to_datetime(stats_df['log_time'])
  • @bigreddot 我发现了这个问题。这是我拥有的列中的值。我正在使用正则表达式进行解析,我构建 DataFrame 的列表在值之前有一个空格 - ['44','65','78']。我删除了空格,现在我有了情节!
  • 太棒了!如果您自行回答并自行接受解决方案,或删除问题以保持[bokeh] 标记整洁,将不胜感激。

标签: python bokeh


【解决方案1】:

问题在于 df 中带有空格的值。一旦我们删除值中的空格,Bokeh 就可以毫无问题地绘制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 2019-08-22
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    相关资源
    最近更新 更多