-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add a cirq.plot() function and SupportsPlot protocol #2097
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
Conversation
|
What is the advantage of this over just calling For example, the advantage of |
|
As described in my proposal #1890 (comment), this design solves the tension between 2 usecases:
This design makes In this design, @dabacon, @mpharrigan, what do you think? |
|
But couldn't that also be solved by having the object have a |
|
You have to do 2 things for a
(1) is repeated logic in every (2) is likely not the best solution, either. We just should not call |
|
I don't think it's much overhead to have if ax is None:
ax = plt.gca()I think this is pretty standard for libraries that build on matplotlib. I still don't think anything should call |
|
I'm hoping that cirq libraries only use object-based matplotlib codes instead of the codes that use "current" or "default" figures like |
|
If someone doesn't pass in an Off the top of my head, I thought of these libraries which offer plotting, and they follow this idiom: |
|
Ping, are you aware of libraries that offer plotting following a different idiom? I would strongly prefer to follow existing patterns when possible. |
|
For now I think the verdict is that we keep plotting as a per-object method. If we find more things that have to be common to the plotting methods, such as e.g. supporting writing to a file in some standard way, then it will make sense to extract a common method. I am particularly wary of the fact that plotting is something that is important outside of cirq, and so cirq cannot really impose a standard way to do it. The benefits would need to be pretty large to override this (e.g. as they are with approx equal). |
|
Let me bring your attention to the Coding Styles recommended by matplotlib. It recommends that the figure and axes be created via functions in the We all like to group data to be plotted into classes. The user's plotting functions become a plotting method of the class, so the data args are unnecessary. What remains is exactly the @Strilanc seems to focus on the I'm open to naming plot methods |
|
Yes, please remove How about we add a |
|
Do you mean something like this? def plot(self, ax, show=False, **kwargs):
...
if show:
ax.get_figure().show()No, I don't want to. Library codes should not call If we agree that all plot methods should take (an |
TIL! Thanks @pingyeh. I still don't think anyone follows this though... They have a lot of gall to suggest we all "do as they say" :) For fun, I was quickly able to find an instance within matplotlib that uses |
|
It is a function in We certainly do not follow recommendation blindly. But this recommendation is something I agree with quite strongly for reasons I wrote above, e.g., lack of side effects, easier to test, etc. |
|
Please drop the global plot method. Write the class instance plot method according to the best practices you outlined. |
Sent #2286 for review. |
) According to the conclusion in #2097. One remaining class is `TomographyResult`, where the `plot()` method creates 2 axes and plots on them, so a single ax arg doesn't work. It will be addressed once a conclusion is reached on how the signature should be (a `Figure`, 2 `Axes`, or something else).
|
Obsoleted by the introduction of conventions around plotting methods. |
This implements what I proposed in #1890 (comment).
SupportsPlotprotocol containing a single_plot_method. Think of it as a graphic version of__str__. It should only plot on the axes given in the arg, and never callshow().cirq.plotfunction that calls_plot_with an axes instance. Think of it as a graphic version ofprint. It creates a new figure and axes if no axes is given, passes the axes to_plot_for rendering, and callsshow. It is intended for interactive use.I migrated
Heatmap,CrossEntropyResult,RabiResultandRandomizedBenchMarkResulttoSupportsPlotprotocol in this PR.TomographyResultlooks a bit more complicated so I left a TODO.Two demo codes and plots are shown below.