Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to show/save the output #28

Closed
Jankowski-J opened this issue Sep 24, 2022 · 4 comments
Closed

How to show/save the output #28

Jankowski-J opened this issue Sep 24, 2022 · 4 comments

Comments

@Jankowski-J
Copy link

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
Copy link

Witold1 commented Oct 13, 2022

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
Copy link
Author

Jankowski-J commented Oct 18, 2022

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
Copy link

Witold1 commented Oct 18, 2022

@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 - #27

@Jankowski-J
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants