Is there any library or module like python matplotlib in Godot ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By subhnand

I am developing a simulation game in Godot which requires drawing line graphs like we can do in Python Matplotlib.

I do not have any clue on how I can achieve this in Godot?

What should be the node to be used? And how do I use the node to plot a line graph based on some equation ?

Any help is appreciated.

Thanks in advance.

:bust_in_silhouette: Reply From: Yuminous

Unfortunately there’s nothing like Matplotlib built-in, but Godot is built around vector math (videogames) so it’s definitely possible but your equations have to be lower level and somewhat more complicated.

There is function specifically to draw custom 2D vectors called _draw() which might be what you’re looking for. Have a look at this tutorial for details on how to utilise it:

There is also Line2D, which is a Node specifically for drawing polyline, maybe that’s all you need for your project?

And other people have written code with Godot specifically for formula graphing:

You should feel free to adapt their code or ideas.

Good luck!

Thanks for your response. I have already gone through this.

My problem is to draw a line graph on a rectangular sprite based on a button click.

How do I approach this ?

Please help.

Thanks in advance.

subhnand | 2021-07-12 11:43

The only complicated bit about that is actually drawing the graph.

I don’t really know how to do exactly what you want, you’ll have to code that yourself.

But basically:

Use a signal from the button to make a UI panel (containing your graph) visible and also have it update (or update beforehand) the Vector2 array (PoolVector2Array) — which contains points for your line graph.

Have a script attached to the UI panel which imports the Vector2 array and uses the _draw() function to draw_multiline(...) with your updated Vector2 array (see here about draw multi-line usage).

I know you’re probably looking for some easier information to follow but drawing Vectors with code not something that can be simplified much. To get started, make a Panel or Sprite, attach a script to it and try out this code:

func _draw():
draw_line(Vector2(0,0), Vector2(300, 100), Color(255, 0, 0), 1)

At least that will get a line on your screen. Then you can work up from there.

Yuminous | 2021-07-12 13:29

There is also the node Line2D, have you looked into it?

That is a simplified version of draw_multiline only requiring the vector data. If _draw is not what you are wanting then it might be a better option.

Yuminous | 2021-07-12 13:36