8000 Add note on writing binary formats to stdout using savefig() by timhoffm · Pull Request #18068 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Add note on writing binary formats to stdout using savefig() #18068

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

Merged
merged 1 commit into from
Feb 17, 2021

Conversation

timhoffm
Copy link
Member

PR Summary

Document how to handle #18049.

@timhoffm timhoffm added this to the v3.3-doc milestone Jul 25, 2020
@@ -2199,6 +2199,10 @@ def savefig(self, fname, *, transparent=None, **kwargs):
:rc:`savefig.format` and the appropriate extension is appended to
*fname*.

To write a binary format like PNG to ``sys.stdout``, use the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems pretty in the weeds to me and adds a technical detail to an already long docstring. I really think the user who wanted to write a png to standard out just didn't realize that a png is not a text format, because I can't see what the use is in dumping the contents to stdout.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could write a program that outputs the binary image and pipe the output to some other program, e.g. python png_stdout.py | convert -trim - result.png.

Maybe not the most common use case, but valid nevertheless.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say thats advanced enough usage that we can assume whoever may want to do that understands how to do the pipe on the python end. I disagree that our docstring should document a python feature.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a documentation problem on our side: We claim to support file-like objects. sys.stdout is one, but it does not work.

Alternatively we'd have to do something like:

fname : str or path-like or binary file-like

which is really terse and possibly not understandable unless you're an expert and already know about https://docs.python.org/3/library/sys.html#sys.stdout

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I won't block. I just disagree that we need to specify what "file-like" means in the context of stdout.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will binary file-like work with svg/eps/etc?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, all formats will work with a binary file-like.

@iljah
Copy link
iljah commented Jul 27, 2020

I disagree that our docstring should document a python feature.

Since matplotlib is written in python, having to document python feature(s) seems reasonable...

@iljah
Copy link
iljah commented Jul 27, 2020

I'd say thats advanced enough usage that we can assume whoever may want to do that understands how to do the pipe on the python end.

That assumption would be wrong in my case, or I'd first assume a bug in matplotlib and open referenced issue

@QuLogic QuLogic modified the milestones: v3.3-doc 8000 , v3.4.0 Jan 28, 2021
@anntzer
Copy link
Contributor
anntzer commented Jan 29, 2021

This is the same error as the one you get when doing e.g. pickle.dump(anything, sys.stdout) so I'd say if stdlib can live with the simple "must be str, not bytes", so can we.
Therefore, I vote to close the PR.

@tacaswell
Copy link
Member

I'm +0 this. It is a useful trick to know, but maybe not worth putting in the savefig docstring? I would definitely merge an example showing this. Either way do we need something saying that the file-like needs to be in binary mode?


While it is true that Matplotlib is written in Python, that does not mean we should fully document Python in our docs.

@anntzer
Copy link
Contributor
anntzer commented Feb 17, 2021

I think the savefig docstring should replace file-like by binary file-like as pointed out by @timhoffm above, but not dwell more on the sys.stdout case: the docstring is already quite long, the use case is not common and the error message is easily googlable (the stackoverflow answer I get on google gives the solution, which is not even anything matplotlib-specific).

An example in the examples directory is fine, I guess.

@timhoffm
Copy link
Member Author

Let's go with the minimal binary file-like then. I'm -0 on an example. This is too savefig-centered. If I wanted to know how to save to stdout, I would look for that in the savefig docs. This would not be easily discoverable in the examples (and it doesn't fit naturally in any of the example categories). Hence, it would mainly add more clutter to the examples.

@timhoffm
Copy link
Member Author

Self-merging based on the positive review.

@timhoffm timhoffm merged commit 12ef486 into matplotlib:master Feb 17, 2021
@timhoffm timhoffm deleted the doc-savefig branch February 17, 2021 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0