e-hulten / july

A small library for creating pretty heatmaps of daily data.
MIT License
209 stars 26 forks source link

How to show/save the output #28

Closed Jankowski-J closed 1 year ago

Jankowski-J commented 1 year ago

Hello, in the examples there are only code snippets to create the objects, but no actual working code to show/print/save images to file.

I am not familiar with matplotlib and I'd appreciate some code examples for that.

Witold1 commented 1 year ago

Hello @Jankowski-J - try matplotlib.pyplot.savefig method (documentation), it's a standard built-in method in matplotlib to save a figure class object. Example: stackoverflow question

Package is a purpose-based wrapper above matplotlib, so all logic and methods are inherited for respective objects.

Jankowski-J commented 1 year ago

I've tried what you have suggested, I've also looked into the properties available in object returned by july.heatmap() method. I can't seem to find any relevant method to save the image.

The heatmap variable is of type AxesSubplot and I don't know how to save that to file.

Below is a snippet of my code:

import numpy as np
from plotly_calplot import calplot

import matplotlib.pyplot as plt

import july
from july.utils import date_range

def main():
    dates = date_range("2020-01-01", "2020-12-31")
    data = np.random.randint(0, 14, len(dates))

    heatmap = july.heatmap(dates, data, title='Github Activity', cmap="github")

    # both of these lines throw an exception
    heatmap.save_fig("saved_hm.png")
    plt.save_fig("test_heatmap.png")

if __name__ == "__main__":
    main()
Witold1 commented 1 year ago

@Jankowski-J - try plt.savefig("test_heatmap.png") instead of plt.save_fig("test_heatmap.png"), you have a typo underscore (..._...) in a name of a method (see documentation). This method will take the current figure (~the last active figure) and save it

heatmap = july.heatmap(dates, data, title='Github Activity', cmap="github")
plt.savefig("test_heatmap.png")

Alternatively, try the next snippet because heatmap object is <class 'matplotlib.axes._subplots.AxesSubplot'>. Matplotlib allows to save figure objects not axes objects (see example stackoverflow question). It also allows to save the selected figure

fig, ax = plt.subplots(1, 1, figsize=(12, 10)) # create figure object and one axes objects
heatmap = july.heatmap(dates, data, title='Github Activity', cmap="github", ax=ax) # plot a chart to ax subplot
fig.tight_layout() # auto adjust the padding between and around subplots.
fig.savefig("saved_hm.png") # save figure

Also, check the dpi and facecolor params to increase the image quality and change the background color (from transparent to needed color code).

Also, there is already a pull request to save it inside the package interface - https://github.com/e-hulten/july/pull/27

Jankowski-J commented 1 year ago

Thank you for your explanation @Witold1 , now I can save the output. I have no further questions, so I will close the issue soon.