-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Added colorscale
to axes.plot()
#3148
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! I have noticed that a visually similar behavior can already be achieved by setting the color of the plot to a gradient, and then specifying the sheen direction. Here is what I mean:
from manim import *
class PlotExample(Scene):
def construct(self):
# construct the axes
axes = Axes(
x_range=[0, 6],
y_range=[-2, 2],
tips=False,
)
curve = axes.plot(
lambda x: 2 * np.sin(2*x),
x_range=(0.0, 6, 0.001),
)
curve.set_color([BLUE, GREEN, YELLOW, ORANGE, RED])
curve.set_sheen_direction(UP)
self.add(axes, curve)
for setting the gradient along the y-axis, and the same code as above, but with a different sheen direction
curve.set_sheen_direction(RIGHT)
for the gradient along the x-axis.
I still like the proposed interface, because it resembles the interface for 3D plots -- however, it might be worth it to implement it using these simpler, and already accessible methods; just from a point of making maintaining a little bit easier. What do you think?
@@ -588,6 +590,8 @@ def plot( | |||
function: Callable[[float], float], | |||
x_range: Sequence[float] | None = None, | |||
use_vectorized: bool = False, | |||
colorscale: Union[Iterable[Color], Color] | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing a single color, which appears to be allowed based on this type hint, yields
ValueError: invalid literal for int() with base 16: ''
Also, the second option (passing an interable containing tuples of the form (color, pivot)
is not captured by this type hint, this should be fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the right way to do it?
colorscale: Union[Iterable[Color], Iterable[Color, float]] | None = None,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#2822 I think this can be typed a little bit more concise in the typing PR
But also it would just be ParsableManimColor
in the future.
manim/manim/utils/color/core.py
Line 402 in d18a8dc
color: ParsableManimColor | list[ParsableManimColor] | None, |
Co-authored-by: Benjamin Hackl <devel@benjamin-hackl.at>
What about the pivots? To me those are key as I set values below 0 to be one color and above 0 to be another color. Is it possible to set pivots with the sheen direction? |
True! I don't think our current implementation of gradients allows to manually set pivots. Maybe this is something that we could consider after we have #3020 and implement our own |
It doesn't work with opengl renderer
|
Can i just say this is hellishly slow ? I am trying to render this and it takes about a second on a good run i think that might be problematic for a lot of cases if people really wanted to use it That is the output i can currently achieve when i use it with opengl. I am not quite sure how your solution works but there might still be something missing. We basically spend 90% of the time just creating mobjects In cairo this doesn't happen for some reason. But 6 seconds for a singular curve is pretty slow in my opinion. |
I would suggest a solution which somehow exploits |
@MrDiver the number of sub-mobjects created is up to the user. It comes from Is that a reasonable solution? It still doesn't work in OpenGL, but that as you point out is because As for using |
I already got the OpenGL thing to work, that's why i have the flamegraph for the 95 seconds. That's what it takes to render this I still think it is a little bit much to create so many mobjects for that. As far as i understand it colors may be applied independently of the number of mobjects. |
class PlotExample(Scene):
def construct(self):
# construct the axes
axes = Axes(
x_range=[0, 6],
y_range=[-2, 2],
tips=False,
)
curve = axes.plot(
lambda x: 2 * np.sin(x), x_range=(0.0, 6), use_vectorized=True
)
colors = [
"#ff0000",
"#ffa500",
"#ffff00",
"#008000",
"#0000ff",
"#4b0082",
"#ee82ee",
]
# curve.set_stroke(colors)
curve.set_color(colors)
# curve.set_fill(colors, opacity=1)
self.play(Write(axes), Write(curve))
self.play(curve.animate.set_sheen_direction(UP))
self.play(curve.animate.set_sheen_direction(RIGHT))
self.play(curve.animate.set_sheen_direction(LEFT))
self.wait() You might wanna take a look at this 👀 i just realized you can do that so it might make realizing the color stuff a lot easier because it is not dependend on the number of curves PlotExample.mp4 |
I added some changes to the PR, you can check if they have the same behavior that you intended. I just reused all of your code. I also added a test to check if the color axis parameter actually works so that it doesn't get missed in the graphical tests. |
Just to reiterate here what was discussed on Discord, I rendered a bunch of videos I have that use this functionality and they all behave the same way as before but render much faster. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to review the Test data again because main changed something i suppose
This is weird because the tests pass on my machine. I even deleted the folder with Manim in it and did a I double and triple checked that I am on the |
Maybe you also needed to run As MrDiver said: the tests needed an update, because they stopped working after certain commit in |
Overview: What does this pull request change?
Added
colorscale
andcolorscale_axis
toaxes.plot()
to allow for colorscales by value for line plots.Motivation and Explanation: Why and how do your changes improve the library?
Adds functionality to the library consistent with
Surface
andOpenGLSurface
.Links to added or changed documentation pages
https://manimce--3148.org.readthedocs.build/en/3148/reference/manim.mobject.graphing.coordinate_systems.CoordinateSystem.html#manim.mobject.graphing.coordinate_systems.CoordinateSystem.plot
Further Information and Comments
Example code:
Reviewer Checklist