我想从 Excel 中取数据,并在 Python 中绘制 2D 核密度估计图,但出现了“ValueError: too many values to unpack (expected 2)” 错误。如何解决?以下是代码:
# libraries
import matplotlib.pyplot as plt
from scipy.stats import kde
import pandas as pd
# create data
x = pd.read_excel(r'C:\Users\Ezra\Desktop\montex.xlsx')
y = pd.read_excel(r'C:\Users\Ezra\Desktop\montey.xlsx')
# Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents
nbins=500
k = kde.gaussian_kde([x,y])
xi, yi = pd.mgrid[x.min():x.max():nbins*100j, y.min():y.max():nbins*100j]
zi = k(pd.vstack([xi.flatten(), yi.flatten()]))
# Make the plot
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), shading='auto')
plt.show()
# Change color palette
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), shading='auto', cmap=plt.cm.Greens_r)
plt.show()