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

Figure Factory #16

Open
ProfFan opened this issue Oct 8, 2020 · 4 comments
Open

Figure Factory #16

ProfFan opened this issue Oct 8, 2020 · 4 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@ProfFan
Copy link

ProfFan commented Oct 8, 2020

Thank you for this awesome library :) Although the schema-based system works really well, I think one of the main hassle in using Plotly.swift is its lack of matplotlib-style plot shorthands like .scatter(x, y), .quiver(x, y, u, v), et cetera. The main Plotly Python API provides figure_factory to automate some of these tasks, for example:

https://plotly.com/python-api-reference/generated/plotly.figure_factory.create_quiver.html

I think it would be awesome if we can have similar functionality so people can start right away using Plotly.swift without having to remember what the schema is for all the different types of figures. I think this could be put in a separate package like PlotlyTools, just like matplotlib.pyplot.

@vojtamolda
Copy link
Owner

Hello Fan,

I'm glad you like and use the library and I certainly appreciate the kind words. Now, regarding your proposal, I think I see two somewhat separate issues:

  • First, a simple scatter plot requires three steps to display it. You need to create a Trace object (i.e. Scatter(...)). Then make the plot itself with the Figure constructor. And finally display the plot with a call to .show(). Certainly not an ideal situation and you're right that this deserves an improvement.

    What do you think about adding a simple .show() method to every trace? This would simplify the .scatter(x, y) factory example to this:

    Scatter(x: someX, y: someY).show()

    To me this hits the sweet spot of being very concise, readable and flexible. It's short, it is explicit about what it does and allows one to alter the plot by modifying the Scatter struct later on.

  • Second, there's certainly a lot of missing plot types. Honestly, I wasn't aware of the figure_factory module in the plotly Python package. The features it offers are really nice but I'm not sure if I can devote so much time to this repository and re-implement it in Swift. So PRs would be definitely welcome ;)

    I checked the source code and for the most part figure_factory combine other more "primitive" traces in useful ways or adapts common data formats to a shape friendlier for plotting.

@vojtamolda vojtamolda added enhancement New feature or request help wanted Extra attention is needed labels Oct 9, 2020
@vojtamolda
Copy link
Owner

BTW, the .show() function in the first bullet point can be implemented with a simple extension of the Trace protocol.

extension Trace {
    @discardableResult
    func show(layout: Layout? = nil, config: Config? = nil) -> Figure {
        let figure = Figure(data: [self], layout: layout, config: config)
        figure.show()
        return figure
    }
}

@ProfFan
Copy link
Author

ProfFan commented Oct 11, 2020

I think this show() method is a great solution! But still, a "easy plotting" module would be really nice. The main problem with the current interface is that you can create plots that is perfectly valid in schema but not actually doing what you want. For example, the axis parameter is really tricky to use as it just doesn't work and there is no error message when you don't understand the inner workings of Plotly.swift and the plotly js library.

vojtamolda added a commit that referenced this issue Oct 13, 2020
- Refactor Trace into a separate file
- Improved documentation
- Simple unit test
@vojtamolda vojtamolda added this to the Plotly.swift 0.4.0 milestone Oct 13, 2020
@vojtamolda
Copy link
Owner

Thanks for your comments, Fan. I really appreciate that you took the time to write your feedback.

  • The Trace.show() and display() methods are already implemented. You can get the 0.4.0 release and check it out for yourself. I also updated the JSON plot schema to the latest version and fixed a few other things.

  • Regarding the Figure Factory idea - I agree with you and it certainly has a place here. I didn't express this point well in my previous comment. It certainly would be great to have.

    For the time being people can built the functionality themselves from the what's already available here. You're right that it's way less convenient but at least it's possible. Unfortunately, I won't be able to work on this anytime soon so I'll leave the issue open with the help wanted tag. Maybe someone will pick it up and do a PR.

You're also right about the axis parameters. Just to keep things organized for myself I opened a new thread in #17. I work on this repo in my free time with frequent gaps and it helps me to figure out what I was doing when I come back after a while.

@vojtamolda vojtamolda removed this from the Plotly.swift 0.4.0 milestone Feb 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants