This is the easiest way to plot functions in python

This is the easiest way to plot functions in python

This is how you can plot mathematical functions in a single line of code in python.

·

3 min read

In this article, you will learn how to plot mathematical functions with a single line of code.

Get Started

First of all, you need to install kiwicalc from pip via the command:

pip install kiwicalc

Then import the following methods:

# for plotting a single function
from kiwicalc import plot_function, plot_function_3d
# for plotting several functions in the same axis system
from kiwicalc import plot_functions, plot_functions_3d
# for plotting several functions in different axis systems
from kiwicalc import plot_multiple

Note: The library is only available in python3, and it currently utilizes Matplotlib for the actual GUI of the plotting.

Short introduction

You can plot functions, algebraic expressions, vectors, and even circles. However, we will focus on functions. We will start with two simple methods: plot_function() and plot_function_3d() . These methods are flexible and can process different kinds of input, but the easiest way is to enter a string in a mathematical syntax. Here are some examples of valid functions that you can enter:

f(x) = -sin(x) + 2cos(2x)"
"g(a,b,c)=a+b+c"
"f(x,y) = x^2 * y^2"
"f(x)=xe^sin(x)-3ln(|4x|)"

Plotting in 2D

Without further ado, here is a tangible example. Let's plot the function :

$$f(x) = x^2$$

plot_function("f(x) = x^2")

And we get:

However, this is not the most efficient way to do this, since the string needs to be parsed internally into a lambda expression. We can simplify this by entering the lambda expression ourselves:

plot_function(lambda x: x**2)

Plotting in 3D

Okay, so we can plot functions with one variable. How do we plot functions with 2 variables? Functions with 2 variables are plotted in 3D, where we generate a grid of x and y values and compute the appropriate z values. For example, let's plot the function

$$g(x,y)=sin(x)*cos(y)$$

plot_function_3d("g(x,y) = sin(x)*cos(y)")

And let's see what we've got!

Plotting several functions in the same axis system

In order to plot several functions, we'll use the plot_functions and plot_functions_3d() methods. These methods will receive a list of strings and plot them together. They can also accept different kinds of input, but let's keep it simple. Here are several examples.

Let's plot the previous function with an additional function:

$$f(x,y) = sin(x)*ln(y)$$

plot_functions_3d(["f(x,y) = sin(x)*cos(y)", "f(x,y) = sin(x)*ln(y)"])

And we get:

Plotting several graphs in the same layout

We can use the plot_multiple method in order to plot several graphs in the same layout. The method receives a list of strings that represent functions. It computes the optimal grid for the graphs and plots all of them on the layout. Here's an example:

plot_multiple(["f(x) = 4x", "f(x) = x^2", "f(x) = x^3", "f(x)= 8", "f(x)=ln(x)", "f(x)=e^x", "f(x)=|x|", "f(x)=sin(x)", "f(x)=cos(x)"])

And the following is displayed:

Plotting surfaces

While this is still a beta feature, I thought it's worth a short mention here. Here's a quick example:

from kiwicalc import Surface
my_surface = Surface("7x + 3y + z + 9 = 0")
my_surface.plot()

Output:

So this is it! If you want to learn more and discover more features, you can visit the official documentation.

You can also contact us on for suggestions and bug fixes.