Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 21068

Irregular grid construction for contour plot

$
0
0

I am trying to construct a contour plot using matplotlib in Python with an irregular xy-grid. My x-data is made up of 10 arrays, each having 500 elements. These 500 elements are different for each of the 10 arrays. An simple example might be x = [[0 1 2 3], [0 1 3 5], [0, 2 4 6]]. The y-data is an array of 10 values. The z-data has the same shape of the x. I hope I've said this clearly (I'm pretty green at coding). I'm also using pickle to write and extract dictionary data:

# Read data from a filewith open('data.pkl', 'rb') as f:    data_loaded = pickle.load(f)x, y, z = data_loaded['eigenenergies'], data_loaded['r_primes'], data_loaded['dc_intensities']

My usual plotting doesn't work.

    # Create meshgridxmesh, ymesh = np.meshgrid(x, y)# Reshape z to match x and y dimensionsN = len(x)M = len(y)z = z.reshape(N, M)# Increase the number of levels for smoother color transitionsnum_levels = 1000  # Adjust as needed# Plot contour#plt.contour(xmesh, ymesh, z)#plt.pcolormesh(x, y, z, shading='flat', vmin=z.min(), vmax=z.max())plt.contourf(xmesh, ymesh, z, levels=np.linspace(z.min(), z.max(), num_levels))# Add color barcolorbar = plt.colorbar()colorbar.set_label(r"Intensity")  # Set the label for the colorbar# Add labels and title (optional)plt.xlabel(r'$E \;\;[J]$')plt.ylabel(r'$r^, \;[\AA]$')plt.title('DC')# Show the plotplt.show()

Any suggestions?

I am not sure how to move forward with this. I tried creating a regular meshgrid by forming a linspace in x, with np.min(x) and np.max(x) as the bounds. I also tried several Copilot prompts to see if AI could give me some idea of where to start, but every suggestion had the x and y data as similar dimensions.


Viewing all articles
Browse latest Browse all 21068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>