首页 > 解决方案 > 空散景图

问题描述

我正在尝试使用散景从我的 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)

我有 print 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)}

任何想法我在阻止绘制值的功能上做错了什么?

标签: pythonbokeh

解决方案


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


推荐阅读