How to plot a single value on contour plot?

Viewed 1099

I am doing a project where I have x_mean, y_mean, z_mean from sensor readings. I have been given task to use the mean values as the z-axis of the plot. In the contour plot, I need the z-axis values of the plot to represent mean value at the X and Y coordinate. So basically I will have 3 different plots.

All I have is x_mean, y_mean, z_mean. So how can I make a contour plot for each of them?

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 Answers

Is there any way to make a contour plot on single value?

TL; DR

No.


To make a contour plot you need a grid of different values, if you set all the values in the grid to a single constant value the algorithm that tries to construct a curve from your data becomes utterly confused

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

and when I say "utterly confused" I really mean it, as you can see from the REPL answering a regular warning and the cryptic line import re ...


Edit

Looking at your last edit, you have 3 time series and you should simply plot them,

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

or maybe, if you are interested in some sort of correlation, use a scatter plot or a 3D scatter plot.

Related