簡體   English   中英

使用 matplotlib 在 Python 中繪制熱圖

[英]Plotting a heatmap in Python using matplotlib

我是 Python 新用戶,需要幫助使用 matplotlib 在 Python 中繪制熱圖

我有三個向量 [x, y, z],每個向量有 7700 個元素。 我從谷歌搜索中獲得了繪制熱圖的代碼(見下文),但最終出現錯誤

一些提示

在數組“x”中,所有項目都不同

在數組“y”中,並非所有值都不同

在數組“z”中,並非所有值都不同

x = mdf_merged.get('Signal_x').samples  # define the x array
y = mdf_merged.get('Signal_y').samples  # define the y array
z = mdf_merged.get('Signal_z').samples  # define the z array

x=np.unique(x)
x = np.unique(x)
y1, yind = np.unique(y, return_index=True)
X,Y = np.meshgrid(x,y[sorted(yind)])
Z=z.reshape(len(y1),len(x), order='F')
plt.pcolormesh(X,Y,Z)
plt.colorbar()
plt.xlabel("X-values")
plt.ylabel("Y-values")

我最終遇到了這個錯誤

Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: cannot reshape array of size 7700 into shape (6447,7700)

所以我的問題是

a) 這可能是什么原因和可能的解決方案?

b) 為什么不能直接取x、y、z。 為什么我必須做 meshgrip 和 reshape ?

我是 Python 的新手,所以如果有更詳細的回復可能會很好

因此,在社區的幫助下,我已經能夠更接近解決方案。 我做的事情是

import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt

x = mdf_merged.get('VariableX').samples
y = mdf_merged.get('VariableY').samples
z = mdf_merged.get('VariableZ').samples
###
xi = np.linspace(min(x),max(x),10)
yi = np.linspace(min(y),max(y),20)
zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='linear')
plt.pcolormesh(xi, yi, zi)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM