Bokeh: Generating graphs in a loop, the output graph's file sizes keep increasing

一曲冷凌霜 提交于 2021-02-08 09:59:12

问题


I'm using bokeh to plot 100 graph files in a loop.

for k in files:
    # Read the log file data into a df.
    log_file_name = str(k) + ".csv" 
    logged_data = pd.read_csv("csv/"+log_file_name, parse_dates=["dttm_utc"], date_parser=dateparse)

    new_logged_data = logged_data.set_index("dttm_utc")
    mean_data = new_logged_data.resample("3D", how=[np.mean])

    # Extract the energy values and time stamps out into two ds.
    energy_data = mean_data["value"]["mean"]
    time_data = mean_data.index

    # Plotting
    output_file("csv/plots/" + log_file_name + ".html", title="Energy Consumption")

    p = figure(width=1600, height=350, x_axis_type="datetime")
    p.line(time_data, energy_data, color='navy', legend='energy')
    #p.circle(time_data, energy_data, size=2, color='navy', alpha=0.2, legend='energy')

    p.title = log_file_name + "   INDUSTRY: " + i + "   SUB-INDUSTRY: " + j)
    p.title_text_font_size = '10pt'
    p.legend.location = "top_left"
    p.grid.grid_line_alpha=0
    p.xaxis.axis_label = 'Time'
    p.yaxis.axis_label = 'Energy (kWh)'
    p.ygrid.band_fill_color="olive"
    p.ygrid.band_fill_alpha = 0.1
    save(p)

    count += 1
    print str(count) + " " + log_file_name

What I'm noticing is that the size of every graph file is more than it's previous one (Please look at the Screenshot 1).

If regenerate all 100 graphs the file sizes are not same as the previous iteration (Screenshot 2).

A strange thing I have also noticed is that if I restart the kernel of my Jupyter notebook then the file sizes reset to the least (Screenshot 3), once again if I repeat regeneration of all 100 files they keep increasing.

The number of samples for generating these graphs is always same then why are file size different? Am I forgetting to do something, like closing the file? Any help!


回答1:


As of Bokeh 0.11.1 it is sometimes necessary to explicitly clear the output with the reset_output function:

from bokeh.plotting import reset_output

The in each iteration:

reset_output()



回答2:


First:

from bokeh.io import curdoc

Then before your plotting block where you have your output file, put in:

curdoc.clear()

This will clear off all of your previous graphs. I'm willing to bet if you look at any file after the first file, you have multiple plots. When I first started with Bokeh, it took me 3 hours of research to figure that out!



来源:https://stackoverflow.com/questions/37290219/bokeh-generating-graphs-in-a-loop-the-output-graphs-file-sizes-keep-increasin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!