Frage

I'm teaching myself about matplotlib and Python and I'm having a difficult time plotting an equation for an elliptic curve. I have the equation down but I'm not doing the y^2

This is as much trouble as I was able to get myself into so far:

from mpl_toolkits.axes_grid.axislines import SubplotZero
import matplotlib.pyplot as plt
import numpy as np
from pylab import *


def plotGraph():
    fig = plt.figure(1)
    ax = SubplotZero(fig, 111)
    fig.add_subplot(ax)

    for direction in ["xzero", "yzero"]:
        ax.axis[direction].set_axisline_style("-|>")
        ax.axis[direction].set_visible(True)

    a = 5; b = 25
    x = np.arange(-50.0, 50.0, 1.0)
    y = pow(x,3) + a*x + b

    xmin = -50; xmax = 50; ymin = -50; ymax = 50
    v = [xmin, xmax, ymin, ymax]
    ax.axis(v)

    ax.plot(x, pow(y,2))

    #grid()
    #ax.grid(color='r', linestyle='-', linewidth=2)

    show()


def main():
    plotGraph()


if __name__ == '__main__':
    main()

The axis() is there because I was also trying to get a clearer-looking chart with grid lines, and I thought that grid() would also take care of that but apparently not. I was also going to try to make it interactive where you click on the points you want and it calculates, but looking over the docs there seems like a lot of interaction mouse options but I don't see mouse interaction that creates some event by clicking on a point on the chart (after the 3rd time reading it over I'm still missing it).

I'm just going from the pyplot summary on matplotlib but I'm not seeing what I'm doing wrong here. The plot of the elliptic curve is way off, not even close.

This is probably a beginner mistake so a junior programmer who takes a second to read this will probably see very quickly why I'm not getting the curve I want.

War es hilfreich?

Lösung

Yes, you're right, you're not doing y^2. To plot elliptic curve in matplotlib I used this code (tested in Python 3):

import numpy as np
import matplotlib.pyplot as plt

def main():
    a = -1
    b = 1

    y, x = np.ogrid[-5:5:100j, -5:5:100j]
    plt.contour(x.ravel(), y.ravel(), pow(y, 2) - pow(x, 3) - x * a - b, [0])
    plt.grid()
    plt.show()

if __name__ == '__main__':
    main()

I've got this plot: enter image description here

Is this what you need?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top