# 2D charge density contour plot (how to get arbitrary 'Z' values in the plot using python)

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.

Used Python Code for the contour plot

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

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()

• Use python interactive plot so that you can use cursor to get value at that point in space Nov 10, 2022 at 23:29
• 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! Jan 31 at 21:59
• 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.
– Sak
Feb 1 at 21:11
• @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. Mar 13 at 16:20

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.

Install and initialize backend

!pip install ipympl

%matplotlib widget

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()

• +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 :) Feb 9 at 16:12
• 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 :) Feb 9 at 19:47