5

I need to draw some curves in Python3 (I am quite used to matplotlib.pyplot) but I have never drawn such things before and I would really appreciate some tips (especially tips how to code it in a "tidy" way) and help.

There is an example (let's use a heart!):

x^2+(5y/4-sqrt[|x|])^2=1  

How can I code such a thing? Should I cut that formula into normal areas and then draw them?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Hendrra
  • 682
  • 1
  • 8
  • 19
  • if you can convert it into `y = f(x)` or `x = f(t), y = g(t)` then maybe you could draw it. – furas Jan 12 '17 at 23:12
  • 2
    This question is more general than the one marked as duplicate. In example: there is no restriction here to use Matplotlib. – Peque Mar 14 '18 at 11:03

1 Answers1

9

According to the equation you show you want to plot an implicit function, you should use contour considering F = x^2 and G = 1-(5y/4-sqrt[|x|])^2, then F-G = 0

import matplotlib.pyplot as plt
import numpy as np

delta = 0.025
xrange = np.arange(-2, 2, delta)
yrange = np.arange(-2, 2, delta)
X, Y = np.meshgrid(xrange,yrange)

# F is one side of the equation, G is the other
F = X**2
G = 1- (5*Y/4 - np.sqrt(np.abs(X)))**2
plt.contour((F - G), [0])
plt.show()

Output: enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241