Home>

To understand the specifications of numpy and matplotlib
I am trying to calculate the vorticity Curl (X, Y) of a velocity vector field (U (X, Y), V (X, Y)) in X, Y two-dimensional space.

The problem i am having

A scatter plot was created by matplotlib.pyplot.plot.scatter with three 1D arrays var1, var2, and var3, which are 2D arrays converted into 1D arrays by flatten (), as arguments.
When I print var3, it is as intended (all values ​​are an array of 2), but when I display it in color with scatter, it is clearly colored with a value other than 2.

  # Make a two-dimensional array one-dimensional
    var1 = X.flatten ()
    var2 = Y.flatten ()
    var3 = Curl.flatten ()
    print (var1, type (var1))
    print (var2, type (var2))
    print (var3, type (var3))
    >[-1. 0. 1. -1. 0. 1. -1. 0. 1.]<class'numpy.ndarray'>
    >[-1. -1. -1. 0. 0. 0. 1. 1. 1.]<class'numpy.ndarray'>
    >[2. 2. 2. 2. 2. 2. 2. 2. 2.]<class'numpy.ndarray'>

    #Scatter plot display
    plt.scatter (var1, var2, c = var3, s = 200, cmap ='rainbow')
    plt.colorbar ()
! [Scatter plot display result] (262f3a21872b2766a2e03a67b3989b72.png)
Corresponding source code
import numpy as np
import matplotlib.pyplot as plt
import math
class var1d:
    def __init__ (self, var_min, var_max, var_N):
        self.min = var_min
        self.max = var_max
        if isinstance (var_N, int) and var_N>1:
            self.N = var_N
        else: else:
            print ('End because the number of grid points var_N is not greater than 1 or not an integer: var_N ='+ str (var_N))
            exit ()
    def array1d (self):
        return np.linspace (self.min, self.max, self.N)
    def diff (self):
        return (self.max --self.min)/(self.N --1)
def main ():
    Create a one-dimensional array by specifying the domain in the #x and y directions and the number of grid points
    x = var1d (-1, 1, 3)
    y = var1d (-1, 1, 3)
    Extend a one-dimensional array of #x, y (size m, n) to a two-dimensional array of m x n
    (X, Y) = np.meshgrid (x.array1d (), y.array1d ())
    # 2 Create a two-dimensional array of variable functions R (X, Y), U (X, Y), V (X, Y)
    R = np.sqrt (X ** 2 + Y ** 2)
    Vel_theta = R * R/(R + 1e-10)
    U = -Vel_theta * Y/(R + 1e-10)
    V = Vel_theta * X/(R + 1e-10)
    Partial derivative of #U dU/dX (X, Y), dU/dX ()
    (dUdY, dUdX) = np.gradient (U, y.diff (), x.diff ())
    Partial derivative of #V dV/dX (X, Y), dV/dX ()
    (dVdY, dVdX) = np.gradient (V, y.diff (), x.diff ())
    #Z-direction component of vorticity
    Curl = dVdX -dUdY
    # Make a two-dimensional array one-dimensional
    var1 = X.flatten ()
    var2 = Y.flatten ()
    var3 = Curl.flatten ()
    print (var1, type (var1))
    print (var2, type (var2))
    print (var3, type (var3))
    #Scatter plot display
    plt.scatter (var1, var2, c = var3, s = 200, cmap ='rainbow')
    plt.colorbar ()
    #drawing
    plt.grid ()
    plt.show ()

if __name__ =='__main__':
    main ()
What I tried

Even if I checked it with plot () instead of scatter (), the distribution was not as intended.

Supplementary information (FW/tool version, etc.)

Python3.6 @ iOS Pythonista

  • Answer # 1

    As another respondent wrote, the cause is that "it looks like 2.0, but there are actually subtle differences."

    --var3 = Curl.flatten ()
    + var3 = np.round (Curl.flatten (), 2)

    Anyway, round the value of the element of var3 to an appropriate number of digits.

    numpy.ndarray.round — NumPy v1.19 Manual

  • Answer # 2

    This is because the values ​​of each element of var3 are not the same.

    When I checked the code of the question in my environment, the value of var3 was as follows.

    [1.9999999997171576 1.9999999996585789 1.9999999997171576
     1.9999999996585789 1.9999999996 1.9999999996585789
     1.9999999997171576 1.9999999996585789 1.9999999997171576]

    When I tried the code below, it was plotted in one color.

    var4 = np.array ([2,2,2,2,2,2,2,2,2])
    plt.scatter (var1, var2, c = var4, s = 200, cmap ='rainbow')
    plt.colorbar ()
    plt.grid ()
    plt.show ()


  • Answer # 3

    It is not a direct answer to this question, but I will write it for reference.

    If you know the range of var3 in advance, you can fix the range of the color map with vmax and vmin.
    If vmax and vmin are not specified, colors will be assigned to the range of the maximum and minimum values ​​of the data, so if there is even a slight difference in the data like this time, a different color will be assigned to the slight difference.
    Draw a Scatter plot with matplotlib

    If vmax and vmin are not specified, for example, 5 when the data range is 0 to 5 and 5 when the range is 0 to 10 will be colored differently.
    If you want to compare graphs of different data, and you want all graphs to display the same data value in the same color, you need to specify vmax, vmin.