0

My variable data is a 3-column data which describes a regular grid of (x, y) and its respective z-value. I would like to represent this data in a contour plot. My code is below:

import tkinter as tk
from tkinter import filedialog
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

root = tk.Tk()
root.withdraw()
filepath = filedialog.askopenfilename()
data = pd.read_excel(filepath)
data = np.array(data, dtype=np.float)

X, Y = np.meshgrid(data[:, 0], data[:, 1])
_X, Z = np.meshgrid(data[:, 0], data[:,2])

plt.figure()
cp = plt.contour(X, Y, Z)
plt.show()

This is what I get:

enter image description here

I do not wish to use any interpolation function as seen in Make contour of scatter since my data is already interpolated.

What am I doing wrong?

Community
  • 1
  • 1
user2554925
  • 487
  • 2
  • 8
  • It may be possible to simply reshape your data, but are you expecting someone to guess the content and structure of your `data`? In case you can provide a clear explanation of your data, or better, provide a [MCVE] it might be possible to help you. – ImportanceOfBeingErnest Jan 28 '17 at 10:47
  • @ImportanceOfBeingErnest, my data is an array of 3-column (tab delimited). e.g. X Y Z 634900 3046900 263,7799 634950 3046900 262,9305 635000 3046900 248,5462 635050 3046900 207,7128 635100 3046900 181,0801 635150 3046900 169,0827 635200 3046900 162,1994 635250 3046900 147,8643 634800 3046950 248,7113 634850 3046950 242,8364 634900 3046950 255,1432 634950 3046950 256,2382 – user2554925 Jan 28 '17 at 10:49
  • If comma separates the lines, this would be four-column array?! – ImportanceOfBeingErnest Jan 28 '17 at 10:53
  • @ImportanceOfBeingErnest, This is an example of my data: X = [3,0,3,-3 ,0,3,-3,0,3,-3,0,3] Y = [-3,-3,-3,-1,-1,-1,1,1,1,3,3,3]   Z = [4.24,3.00,4.24,3.16,1.00,3.16,3.16,1.00,3.16,4.24,3.00,4.24] – user2554925 Jan 28 '17 at 11:00
  • 1
    This gets close to a [MCVE]. So in this case the underlying structure will require the arrays to plot to be of shape `(4,3)`. You can reshape them by `X=np.array(X).reshape((4,3)) Y=np.array(Y).reshape((4,3)) Z=np.array(Z).reshape((4,3))` – ImportanceOfBeingErnest Jan 28 '17 at 11:06
  • Marking this duplicate isn't helpful. The linked answer builds a function around `griddata` which **_is_** interpolation. – Lucas Mar 20 '17 at 02:07

0 Answers0