Search code examples
pythonnumpyhilbert-curve

Splitting X Y coordinates in list for MatPlotLib


I'm working on a program that involves the use of the Hilbert curve python module. The output of the Hilbert curve python module comes in the form of a list, or in the form of a single point set. I'm having trouble separating the list so that I can plot it using matplotlib.

Here's the sample code:

import numpy as np
from hilbertcurve.hilbertcurve import HilbertCurve
import regex as re


p=int(input("order of curve: "))
n=2
hilbert_curve = HilbertCurve(p,n)

totalpoints = 2**(p*n)

distances = list(range(totalpoints))
#distances = list(range(int(input("list the number of points"))))

points = hilbert_curve.points_from_distances(distances)


#This stuff below prints out all of the individual points
#for point, dist in zip(points, distances):
#    print (f'point(h={dist}) = {point}')

#this prints out the array of all points generated by the program. Useful for debug
#print(points)

The issue is, while using matplotlib it doesn't separate the individual points in 'points' resulting in multiple linear plots instead of a Hilbert curve.

I also need to be able to separate them so I can perform arithmetic on the individual points. This library infinitely expands the length of the curve rather than defining it within a single 1x1 square how the curve should be represented. This is something easily resolved by dividing the individual points.

The end result of the product is to be able to determine a starting point for the Hilbert curve instead of having it start at 0,0. The second implementation will be in a point finding script where the generated points of a Hilbert curve can be checked against specific points to see if the curve passes through it. So, for both implementations I need to be able to manipulate the individual points which I can't in the current list form.

I have attempted to use the np split() as well as the str.split() methods. the str methods fails due to this being a list, not a string. np isn't working for an unknown reason.


Solution

  • this should do what you want but is very inefficient if you have millions of points (6 digits):

    import matplotlib.pyplot as plt
    
    points = [[i,i**2] for i in range(10)]
    xs=[]
    ys=[]
    for x,y in points:
        xs.append(x)
        ys.append(y)
    
    plt.figure()
    plt.plot(xs,ys)
    plt.show()
    

    in this case, I suspect many of your points will be closer than 1px to each other, so instead you can add your points to a kd-tree (if your points are in 2 dimensions, which they seem to be, K will be 2) and query the points that are in your view port (assuming you have zoomed in to see the actual curve, instead of just a solid block of points, since that's what the Hilbert curve is doing(it is a space-filling curve)).

    Imagine the same scenario for a fractal pattern (which a Hilbert curve is really) that has infinitely many points due to its recursive nature. You won't be drawing points that are nanometers apart when the pixels on your display are in the order of micrometers since they won't be visiblle.