2
$\begingroup$

I need to generate a contour plot from a file (named abc.gnu) which has X, Y and Z columns of data. It has 1000000 rows of data. I have got the contour plot with a python code (shown at the end). What I need is to know the 'Z' values in any arbitrary place of choice (or to see 'Z' values in different regions of the contour plot). If I zoom any particular region, still the color bar is showing the same min and max values, so I can't get any information of 'Z' values in the zoomed region as well. Do you have any idea to solve this for my used file. I have given my used python script at the end. Any other idea to get more effective 2D contour plot is also welcome.

enter image description here

Used Python Code for the contour plot

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

data=pd.read_csv('abc.gnu',delimiter='      ',header=None,engine='python')
data_M=np.array(data)

X=data_M[:,0]
Y=data_M[:,1]
Z=data_M[:,2]
Xs=X.reshape((1000,1000))
Ys=Y.reshape((1000,1000))
Zs=Z.reshape((1000,1000))

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

cp = plt.contourf(Xs, Ys, Zs)
plt.colorbar(cp)

ax.set_title('Contour Plot')
ax.set_xlabel('X')
ax.set_ylabel('Y')

x = np.linspace(0, 5, 150)
y = np.linspace(0, 5, 150)

X, Y = np.meshgrid(x, y)

plt.xlim(0, 25)
plt.ylim(0, 4)

plt.show()
$\endgroup$
4
  • 2
    $\begingroup$ Use python interactive plot so that you can use cursor to get value at that point in space $\endgroup$ Commented Nov 10, 2022 at 23:29
  • 1
    $\begingroup$ I gave my +1 long ago, but just wanted to come back and check how things are going. Did you figure this out? Are you still working on it? Let us know! @Pranavkumar do you think you can extend that comment into an answer? It would be nice to get this question out of the unanswered queue! $\endgroup$ Commented Jan 31, 2023 at 21:59
  • $\begingroup$ Thanks @NikeDattani for the follow up. Still not figured out. It would be great if Pranav kumar or anyone else could suggest in an extended comment to an answer. $\endgroup$
    – Sak
    Commented Feb 1, 2023 at 21:11
  • 1
    $\begingroup$ @Sak I have made an answer. Please check if it answers your question. If it does please click on the tick mark next to it. You also get +2 if you do it. Also acknowledges the effort that goes into answering a question. $\endgroup$ Commented Mar 13, 2023 at 16:20

1 Answer 1

4
$\begingroup$

This can be achieved with a 3d interactive contour plot.
Here I have higlighted the current cursor position with a red dot.
It gives you all the x, y, z values at that point. The below plot is generated with a small sample of your data from the screenshot of the x,y,z values. I am plotting on a google colab notebook.

3d_contour_plot

Install and initialize backend

!pip install ipympl

%matplotlib widget

from google.colab import output
output.enable_custom_widget_manager()

Main code:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits import mplot3d

data= [ [0,0,0.254], [0, 0.14,0.27], [0,0.29, 0.30], [0.1, 0.43, 0.31]] # Using a subset of your data
data_M = np.array(data)

X=data_M[:,0]
Y=data_M[:,1]
Z=data_M[:,2]

# change to 1000 for your original script
Xs=X.reshape((2,2))
Ys=Y.reshape((2,2))
Zs=Z.reshape((2,2))

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(Xs, Ys, Zs)

ax.set_title('Contour Plot')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()
$\endgroup$
2
  • 2
    $\begingroup$ +1 again! Since you said that you love Python I was going to send you a bunch of unanswered python questions that I thought you might be able to answer, with this one being on the top of the list! A bigger challenge though would be my most recent question :) $\endgroup$ Commented Feb 9, 2023 at 16:12
  • 2
    $\begingroup$ if this question was worth answering, might it be worth upvoting too? Since the score is 1 and I upvoted, I know that others didn't :) $\endgroup$ Commented Feb 9, 2023 at 19:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .