📜  Python中的 Matplotlib.pyplot.hexbin()函数

📅  最后修改于: 2022-05-13 01:55:42.460000             🧑  作者: Mango

Python中的 Matplotlib.pyplot.hexbin()函数

Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 PyplotMatplotlib模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。在 Pyplot 中可以使用各种图,包括线图、等高线图、直方图、散点图、3D 图等。

Matplotlib.pyplot.hexbin()函数

matplotlib 库的 pyplot 模块中的hexbin()函数用于制作点 x、y 的二维六边形分箱图。

下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.hexbin()函数:

示例 1:

Python3
# Implementation of matplotlib function 
import matplotlib.pyplot as plt 
import numpy as np 
    
np.random.seed(19680801) 
    
n = 100000
x = np.random.standard_normal(n) 
y = 12 * np.random.standard_normal(n) 
     
plt.hexbin(x, y, gridsize = 50, cmap ='Greens') 
plt.title('matplotlib.pyplot.hexbin() Example') 
plt.show()


Python3
# Implementation of matplotlib function 
import matplotlib.pyplot as plt 
import numpy as np 
    
np.random.seed(19680801) 
    
n = 100000
x = np.random.standard_normal(n) 
y = 2 * np.random.standard_normal(n) 
z =[1, 2, 3, 4] 
xmin = x.min() 
xmax = x.max() 
ymin = y.min() 
ymax = y.max() 
    
hb = plt.hexbin(x, y, gridsize = 50, 
               bins = z, cmap ='BuGn') 
    
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
    
cb = plt.colorbar(hb) 
cb.set_label(z)
plt.title('matplotlib.pyplot.hexbin()\
Example')
  
plt.show()


输出:

示例 2:

Python3

# Implementation of matplotlib function 
import matplotlib.pyplot as plt 
import numpy as np 
    
np.random.seed(19680801) 
    
n = 100000
x = np.random.standard_normal(n) 
y = 2 * np.random.standard_normal(n) 
z =[1, 2, 3, 4] 
xmin = x.min() 
xmax = x.max() 
ymin = y.min() 
ymax = y.max() 
    
hb = plt.hexbin(x, y, gridsize = 50, 
               bins = z, cmap ='BuGn') 
    
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
    
cb = plt.colorbar(hb) 
cb.set_label(z)
plt.title('matplotlib.pyplot.hexbin()\
Example')
  
plt.show()

输出: