Search code examples
pythonseaborndata-visualizationnumber-theory

Visualizing gaussian primes and gaussian prime factors in Python


I want to create two plots, one that has gaussian primes depicted in complex plane and another one that shows the number of (gaussian) prime factors of that complex number. I can calculate both the primality (the first scenario) and the factors (the second one), but can't find the correct format for the visualization.

In the first case I want to merely draw a tick for every prime in a complex plane of given dimensions, like pictured in this Wolfram Mathworld page. I have already defined a function with the following signature: is_gaussian_prime(c : complex) -> bool. What I'm missing is a suitable data structure for storing the information of the primacy of all the complex numbers from real_min to real_max and imag_min to imag_max and a way to visualize that data structure. I have looked into seaborn.relplot() as documented here, but cannot quite grasp, what kind of shape I want the data to be in.

The reason I want to use relplot() is because that allows drawing different sizes of ticks for different amount of prime factors in the second scenario. Meaning that I want to draw the second plot in such a way that the size of the tickers represent the number of prime factors of that particular gaussian number. To be more precise, in the second plot I want to draw gaussian primes with a distinct color and gaussian composites as black with bigger ticker size for composites that have more factors. For this case I have a function of the following signature already defined: number_of_gaussian_factors(c : complex) -> int.


Solution

  • You could generate a list of gaussian primes, and then call sns.scatterplot on the real and imaginary parts:

    from matplotlib import pyplot as plt
    import seaborn as sns
    import numpy as np
    from sympy.ntheory.primetest import is_gaussian_prime
    
    N = 200
    prime_set = set()
    for i in range(N):
        for j in range(N):
            if is_gaussian_prime(i + j * 1j):
                prime_set.add(i + j * 1j)
                prime_set.add(i - j * 1j)
                prime_set.add(-i + j * 1j)
                prime_set.add(-i - j * 1j)
    primes = np.array(list(prime_set))
    plt.figure(figsize=(15, 15))
    ax = sns.scatterplot(x=np.real(primes), y=np.imag(primes), color='tomato', s=20)
    ax.set_aspect('equal')
    plt.show()
    

    sns.scatterplot of gaussian primes

    Calculating the number of prime factors seems a bit more complicated, and a bit less symmetric (e.g. 2=(1+i)(1-i) and 2i=i(1+i)(1-i)). You have an array of gaussian integers (e.g. gaussian_ints) and accompanying array of number of factors (e.g. num_factors), you could call e.g. sns.scatterplot(x=np.real(primes), y=np.imag(primes), size=num_factors, hue=num_factors).