How to draw cubic plane curve?

684 Views Asked by At

In Python, using MatPlotLib, given [vector] parameters $a$ and $b$ and [scalar] parameter $c$, I want to draw a general cubic plane curve in 2-dimensional space (regular plane with $x$ and $y$ axes): $$ \sum\limits_{i=1}^3 \left( a_i \cdot x^i + b_i \cdot y^i \right) + c = 0. $$

How can I draw a part of this curve with constraints $(x, y) \in [0; 1]^2$?

1

There are 1 best solutions below

0
On BEST ANSWER

I've found a solution for MatPlotLib: matplotlib.pyplot.contour allows to draw contour set by implicit equation. Here are answers from StackOverflow:

https://stackoverflow.com/questions/4680525/plotting-implicit-equations-in-3d https://stackoverflow.com/questions/4690471/plotting-system-of-implicit-equations-in-matpotlib https://stackoverflow.com/questions/2484527/is-it-possible-to-plot-implicit-equations-using-matplotlib

Here is my solution on Python 2.7:

from random import random
from numpy import linspace
from numpy.random import random as random_array
from matplotlib import pyplot as plt

LEFT_X = -2
RIGHT_X = 2
LIMITS_X = (LEFT_X, RIGHT_X)

UPPER_Y = 2
BOTTOM_Y = -2
LIMITS_Y = (BOTTOM_Y, UPPER_Y)

RESOLUTION = 1000

def get_poly(x):
    return (x**3, x**2, x)

a = (random_array(3) - .5) * 10
b = (random_array(3) - .5) * 10
c = (random()        - .5) * 2

x = linspace(LEFT_X, RIGHT_X, RESOLUTION)
y = linspace(BOTTOM_Y, UPPER_Y, RESOLUTION)
x_poly = get_poly(x)
y_poly = get_poly(y)

eq = a.dot(x_poly) + b.dot(y_poly)[:,None] + c

plt.contour(x, y[:,None].ravel(), eq, [1])
plt.show()

Image with 2-dimensional cubic plane curve example