0

I am trying to plot a contour plot with Python's Matplotlib package. I'm trying to get similar results to what can be seen in this other stack overflow post. However, I'm getting the problem of it saying that there is a type error and it tells me TypeError: Invalid dimensions for image data, which can be seen in the full error code below.

Traceback (most recent call last):
    File "./plot_3.py", line 68, in <module>
        plt.imshow(zi, vmin=temp.min(), vmax=temp.max(), origin="lower", extent=[x.min(), x.max(), y.min(), y.max()])
    File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 3022, in imshow
**kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/__init__.py", line 1812, in inner
        return func(ax, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/axes/_axes.py", line 4947, in imshow
        im.set_data(X)
    File "/usr/lib64/python2.7/site-packages/matplotlib/image.py", line 453, in set_data
        raise TypeError("Invalid dimensions for image data")
TypeError: Invalid dimensions for image data

I'm unsure of what this means, as googling brought up no useful results on how to fix it. The code is listed below, and the data that I'm using can be found here. The code below simply runs the code which will parse the file and then return the data to the main where it's supposed to plot it then. To run the code, you have to use ./plot_3.py 20.0 to use it with the specific file that I posted above. x ranges from 0 to 0.3 with 61 grids, while y ranges from 0 to 0.4 with 81 grids. The data is in the format x,y,temperature where I want the temperature values to be the contour values.

from __future__ import print_function, division
import math
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import sys
import matplotlib.cm as cm
from matplotlib.mlab import griddata
import scipy.interpolate

def ParseFile(filename):
    x = []
    y = []
    temp = []
    infile = open(filename, 'r')

    lines = [line.strip() for line in infile.readlines()]

    for line in lines:
        x.append(float(line.split(',')[0]))
        y.append(float(line.split(',')[1]))
        temp.append(float(line.split(',')[2]))

    return np.array(x), np.array(y), np.array(temp)

time = str(sys.argv[1])
filename = time + "_sec.dat"

x,y,temp = ParseFile(filename)
xi = np.linspace(min(x), max(x))
yi = np.linspace(min(y), max(y))
zi = scipy.interpolate.griddata((x,y),temp,(xi,yi),method="linear")

matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'
plt.imshow(zi, vmin=temp.min(), vmax=temp.max(), origin="lower", 
           extent=[x.min(), x.max(), y.min(), y.max()])
plt.colorbar()
plt.show()
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Cmertin
  • 141
  • 1
  • 3
  • 11

1 Answers1

0

I think the problem is that that you need to have the points to be interpolated in a gridded format, not two 1D matrices for the interpolate.griddata function.

Adding this line to the (xi, yi) declaration I think fixes your problem:

x,y,temp = ParseFile(filename)
xi = np.linspace(min(x), max(x))
yi = np.linspace(min(y), max(y))
#create the 2D grid for interpolation:
xi, yi = np.meshgrid(xi,yi)
zi = scipy.interpolate.griddata((x,y),temp,(xi,yi),method="linear")
Lucas Currah
  • 418
  • 4
  • 8