如何在轮廓图上绘制单个值?

时间:2020-07-26 12:03:31

标签: python numpy matplotlib plot contour

我正在做一个项目,我从传感器读数中得到x_mean,y_mean,z_mean。我的任务是使用平均值作为绘图的z轴。在轮廓图中,我需要曲线的Z轴值来表示X和Y坐标处的平均值。所以基本上我将有3个不同的情节。

我只有x_mean,y_mean,z_mean。那么我如何为它们中的每一个绘制轮廓图?

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(6,5))
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
ax = fig.add_axes([left, bottom, width, height]) 

start, stop = 0, 5

x_vals = np.linspace(start, stop)
y_vals = np.linspace(start, stop)
X, Y = np.meshgrid(x_vals, y_vals)
print(X)
print(X.shape)
x_mean = -19.44287213
Z = x_mean
print(Z)
print(Z.shape)

cp = plt.contourf(X, Y, Z, 80)
plt.colorbar(cp)

ax.set_title('Contour Plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()

1 个答案:

答案 0 :(得分:0)

有什么办法在单个值上绘制轮廓图?

TL; DR

否。


要绘制等高线图,您需要一个包含 不同 值的网格,如果将网格中的所有值都设置为单个常数,则该算法将尝试构造您数据中的曲线变得完全混乱

In [19]: x = np.linspace(0,5)
    ...: X,Y = np.meshgrid(x,x) 
    ...: Z = np.ones(X.shape) # a constant matrix
    ...: c = plt.contour(X,Y,Z, levels=[Z.mean()])                                                                                    
/home/boffi/lib/miniconda3/bin/ipython:4: UserWarning: No contour levels were found within the data range.
  import re

当我说“完全困惑”时,我的意思是真的,正如您在REPL回答中所看到的那样 常规警告和神秘线import re ...


编辑

查看您的上一次编辑,您有3个时间序列,您应该简单地绘制它们,

plt.plot(t, x_mean, y_mean, z_mean)

或者,如果您对某种相关性感兴趣,请使用散点图或3D散点图。