How to use matplotlib/numpy to plot heatmap given three irregular lists of values in Python

2024/11/16 9:45:22

I'm wondering if there is a way to use matplotlib and numpy to plot the heatmap of three lists. My grid is not regular, it is oddly shaped so this does not work for me: Plotting a Heat Map X,Y,Intensity From Three Lists. When I try that, I get ValueError: cannot reshape array of size 1906 into shape (1847,127). My lists have multiple of the same X and Y values, and are not in any way rectangular. I was wondering if there is a way to plot my data so it looks like the meshgrid/imshow grids that you can get when you have rectangular data. Basically, I just want to plot a bunch of given intensities at given X, Y values and have them show as a heatmap.

Thanks!

edit: Here is the kind of data that I am working with. I'm trying to use the 4th column as the intensity and the 1st and 2nd columns as the x and y respectively. https://pastebin.com/XCcwRiJn

Answer

Thanks ymmx, I'm new to Stack Overflow so I don't know how to turn your answer into the actual answer. Here's what I did to make it work using a 2D iterpolation:

myData = np.genfromtxt(fileName, delimiter=",")
X = myData[:, 0]
Y = myData[:, 1]
intensity = myData[:, 3]
XY = np.column_stack((Y,X))
grid_x, grid_y = np.mgrid[-.2:.2:100j, 0:.05:200j]
grid1 = griddata(XY, intensity, (grid_x, grid_y), method='cubic')
plt.imshow(grid1.T, extent=(0, .5, 0, .05), origin='lower',cmap='jet')

If someone has this same problem, you have to adjust your mgrid and extent values to fit your data.

Thanks for the help!

https://en.xdnf.cn/q/118946.html

Related Q&A

How to set custom color for symbols like*,#,etc in python tkinter

How to set specific color for certain symbols like ,#,etc example if I type "" its color should be blue and other stay remain same. typedecker sir i am binding you function like this but thi…

How can I get only heading names.from the text file

I have a Text file as below:Education: askdjbnakjfbuisbrkjsbvxcnbvfiuregifuksbkvjb.iasgiufdsegiyvskjdfbsldfgdTechnical skills : java,j2ee etc.,work done: oaugafiuadgkfjwgeuyrfvskjdfviysdvfhsdf,aviysdvw…

Concatenate list elements that fall between list elements of certain value

Imagine I have the following list:>>> mylist[(a, uDT),(Satisfactory, uJJ),(tracing, uVBG),(with, uIN),(a, uDT),(fairly, uRB),(persistent, uJJ),(with, uIN)]How do I concatenate list items that …

How to create list from 100 elements to list of 10 [duplicate]

This question already has answers here:How to iterate over a list in chunks(40 answers)Closed 4 years ago.I have small brain fade today and I believe it will be faster to get hint here than wondering f…

how to make the width of image longer using animation in tkinter GUI?

I have this image and I want to make the width of this image longer till the end of the window of tkinter using animation but I havent got any proper way to achieving this task. any suggestions ?

Syntax error with if statement in python 2.7

Im having trouble with a "Syntax Error: invalid syntax" message in python code that keeps moving the goals posts on me. Heres the sample code:another_answer = int(raw_input("> "…

Python- Quicksort program in-place

I have a quicksort program here, but there seems to be a problem with the result. I think there must have been some issue in the areas highlighted below when referencing some values. Any suggestions?#…

Maximum recursion error in `__eq__` implementation

class Coordinate(object):def __init__(self,x,y):self.x = xself.y = ydef getX(self):# Getter method for a Coordinate objects x coordinate.# Getter methods are better practice than just accessing an attr…

python swig : ImportError wrong ELF class: ELFCLASS64

I am trying to interface from python to c++ code via swig. I get the following error , while trying to execute my script.File "./driver.py", line 4, in <module>from fixMessageSim import…

cnn news webscraper return empty [] without information

so i wrote this code for now: from urllib import request from bs4 import BeautifulSoup import requests import csv import reserch_term = input(What News are you looking for today? )url = fhttps://editi…