0

I wrote a Python code for generating a hexbin plot and I would like to save it in a image file but it's impossible, the code creates a file but with nothing inside.

import pandas as pd
import matplotlib.pyplot as plt

output_report = 'H:\_JUPYTER\_report\\'
df = pd.read_csv('H:\\_JUPYTER\\_report\\matrix_delta_speed.csv.bz2', compression='bz2', index_col='date', parse_dates=['date'], date_parser=lambda col: pd.to_datetime(col, errors='coerce'))

x = df.speed
y = df.delta

fig, ax = plt.subplots(ncols=1, sharey=True, figsize=(17, 10))
hb = ax.hexbin(x, y, gridsize=(150,150), cmap='inferno')
ax.axis([x.min(), x.max(), y.min(), y.max()])
ax.set_title('Delta vs. Speed')
ax.set_xlabel('Speed')
ax.set_ylabel('Delta')
cb = fig.colorbar(hb, ax=ax)
cb.set_label('counts')

plt.show()

plt.savefig(output_report+'Matrix.png', dpi=600)

I process to savefig but the file is completely white:

enter image description here

enter image description here

It's the same case for "hist" and other plot.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
PhP60
  • 13
  • 5
  • 1
    `output_report='H:\_JUPYTER\_report\\'` doesn't look good to me. Maybe `output_report='H:\\_JUPYTER\\_report\\'` – Quang Hoang May 19 '20 at 19:30
  • No that's good for me! Strange behavior on Windows. When you use a global variable for a path, you need to put double backslash at the end. When you set directly the path in a string as argument, you need to put all in double backslahs. The file is correctly create, but nothing inside. Thanks – PhP60 May 19 '20 at 19:34
  • 1
    May be call `savefig()` BEFORE `show()` – david May 19 '20 at 20:12
  • 1
    You might also want to check out *r* string literals, eg [here](https://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-and-what-are-raw-string-literals). – tom10 May 19 '20 at 20:42
  • I try to do savefig() before and.... it's working! Very strange but why not! Thanks – PhP60 May 20 '20 at 12:32

0 Answers0