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 twodimensional space.
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 twodimensional array onedimensional
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 onedimensional 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 onedimensional array of #x, y (size m, n) to a twodimensional array of m x n
(X, Y) = np.meshgrid (x.array1d (), y.array1d ())
# 2 Create a twodimensional 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 + 1e10)
U = Vel_theta * Y/(R + 1e10)
V = Vel_theta * X/(R + 1e10)
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 ())
#Zdirection component of vorticity
Curl = dVdX dUdY
# Make a twodimensional array onedimensional
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

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 matplotlibIf 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.
Related articles
 python  the web browser is garbled and the web application is not displayed
 results are not displayed in python
 python  anaconda is not displayed in the terminal on mac
 python  problem that css is not loaded in html and cannot be displayed during web application creation
 python  images are not displayed in the flask app launched with docker
 i want to make a python array onedimensional, but it doesn't work
 python  object is not displayed in html in django queryset
 python  why is name'beam' is not defined displayed
 python  i want to hide the result of gacha that is displayed as 0
 python  image is not displayed in tkinter module
 python  image is not displayed with skimageioshow ()
 python 3x  i want to make a scatter plot of the results of regression analysis
 [python] even if i run the script, only "python" is displayed and it does not work
 python  when i use write (), the characters are not displayed
 image cannot be displayed with python tkinter
 python  convert a onedimensional array to a twodimensional array
 python  i want to divide the contents displayed from the database by user
 python  labels are not displayed in matplotlib
 python : How to set aside the formula in text format?
 python : Space of elementary outcomes through NUMPY
 python : How to move all the .csv files from the folder and get correlations between files?
 python : Find the desired element in the NUMPY array
 python : The optimal way to pass the sliding window by DataFrame creating a normalized list
 python : Using Scatter with Matplotlib, "__init __ () Missing 1 Required Positional Argument: 'figure'" error occurs
 python : Rounding to a smaller side of up to 2 silent signs
 python : Matplotlib.pyplot: How to deploy divisions on the axes y and x in the graph of the graph?
 In Python Resulting Train Set Will Be Empty: How to "repair" sample size?
 python : Latex does not handle the entire text on the MATPLOTLIB.pyplot graph
As another respondent wrote, the cause is that "it looks like 2.0, but there are actually subtle differences."
Anyway, round the value of the element of var3 to an appropriate number of digits.
numpy.ndarray.round — NumPy v1.19 Manual