3

I like to use matplotlib to exhibit science experiment result, and I want to storage those pictures to MongoDB.
Although I had read the matplotlib official document, I can't find any method to get a file-like object can easily storage into GridFS.
A viable solution is to save the figure to a png file, then upload this png file to GridFS. Is there any more succinct method?

1

1 Answer 1

3

I solved this problem.

def plot_fig(file_name):
    x = np.linspace(0, 10, 10)
    y = np.linspace(0, 10, 10)
    plt.plot(x, y)

    from io import BytesIO
    figfile = BytesIO()
    plt.savefig(figfile, format='png')

    fs = GridFS(db)
    try:
        f = fs.new_file(filename=file_name)
        f.write(figfile.getvalue())
    finally:
        f.close()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.