简体   繁体   中英

Plot contourf with 3 vectors

I'm trying to plot contourf of 3 different equal vectors. I learned i have to interpolate them so i can obtain a function connecting them. But i have no idea how to interpolate or use contourf. I'm using this code:

from scipy.interpolate import griddata

[X, Y] = np.meshgrid(np.unique(x), np.unique(hf));
Z = griddata(x, hf, Sf, X, Y);
plt.contourf(X, Y, Z);

But it's not working, it's returning me: "Unknown interpolation method array....". I'm new to Python and i'll appreciate any help. Thank you !

Sample of my vectors:

hf=[1.09761983 1.09761983 1.09703788 1.097271   1.10385924 1.11355334
 1.12989642 1.14784403 1.16748527 1.18743366 1.20757876 1.22775466
 1.24794379 1.26813503 1.28832711 1.30851956 1.32871231 1.34890535
 1.36909866 1.38929224 1.40948608 1.42968017 1.44987451 1.47006907
 1.49026387 1.51045888 1.5306541  1.55084953 1.57104516 1.59124097
 1.61143697 1.63163315 1.6518295  1.67202601 1.69222269 1.71241952
 1.73261649 1.75281362 1.77301088 1.79320827 1.81340579 1.83360344
 1.85380121 1.8739991  1.8941971  1.9143952  1.93459341 1.95479173
 1.97499014 1.99518865 2.01538725 2.03558594 2.05578471 2.07598357
 2.09618251 2.11638152 2.13658061 2.15677978 2.17697901 2.19717832
 2.21737769 2.23757712 2.25777662 2.27797617 2.29817579 2.31837546
 2.33857519 2.35877497 2.3789748  2.39917468 2.41937461 2.43957459
 2.45977462 2.47997469 2.5001748  2.52037495 2.54057515 2.56077538
 2.58097566 2.60117597 2.6213763  2.64157661 2.66177664 2.68197522
 2.70216771 2.72233703 2.74242962 2.76229069 2.78156777 2.79947929
 2.81495902 2.82619235 2.83289165 2.8341084  2.8333398  2.83220409
 2.83188838 2.83287856 2.83369273 2.83369273]


sf=[-0.00000000e+00  9.41936214e-05  1.91152978e-04  2.98759982e-04
  3.99659269e-04  4.70022026e-04  5.16169870e-04  5.32537982e-04
  5.37949242e-04  5.35608036e-04  5.31283264e-04  5.26012479e-04
  5.20586048e-04  5.15141534e-04  5.09744458e-04  5.04407257e-04
  4.99135214e-04  4.93930450e-04  4.88794377e-04  4.83727997e-04
  4.78732027e-04  4.73806938e-04  4.68952988e-04  4.64170250e-04
  4.59458631e-04  4.54817895e-04  4.50247680e-04  4.45747516e-04
  4.41316836e-04  4.36954993e-04  4.32661270e-04  4.28434890e-04
  4.24275023e-04  4.20180801e-04  4.16151315e-04  4.12185633e-04
  4.08282794e-04  4.04441824e-04  4.00661733e-04  3.96941521e-04
  3.93280183e-04  3.89676711e-04  3.86130097e-04  3.82639334e-04
  3.79203420e-04  3.75821360e-04  3.72492166e-04  3.69214858e-04
  3.65988469e-04  3.62812041e-04  3.59684630e-04  3.56605303e-04
  3.53573142e-04  3.50587242e-04  3.47646715e-04  3.44750685e-04
  3.41898293e-04  3.39088693e-04  3.36321058e-04  3.33594572e-04
  3.30908438e-04  3.28261874e-04  3.25654112e-04  3.23084400e-04
  3.20552001e-04  3.18056194e-04  3.15596272e-04  3.13171543e-04
  3.10781329e-04  3.08424968e-04  3.06101811e-04  3.03811222e-04
  3.01552581e-04  2.99325279e-04  2.97128722e-04  2.94962327e-04
  2.92825525e-04  2.90717754e-04  2.88638441e-04  2.86586924e-04
  2.84562119e-04  2.82561312e-04  2.80576100e-04  2.78580803e-04
  2.76501340e-04  2.74154196e-04  2.71113974e-04  2.66628934e-04
  2.59338319e-04  2.47990830e-04  2.30648251e-04  2.08333235e-04
  1.80857088e-04  1.52747034e-04  1.25846945e-04  1.00061809e-04
  7.56242555e-05  5.11214342e-05  2.54090569e-05 -0.00000000e+00]


x=[ 0.          0.1010101   0.2020202   0.3030303   0.4040404   0.50505051
  0.60606061  0.70707071  0.80808081  0.90909091  1.01010101  1.11111111
  1.21212121  1.31313131  1.41414141  1.51515152  1.61616162  1.71717172
  1.81818182  1.91919192  2.02020202  2.12121212  2.22222222  2.32323232
  2.42424242  2.52525253  2.62626263  2.72727273  2.82828283  2.92929293
  3.03030303  3.13131313  3.23232323  3.33333333  3.43434343  3.53535354
  3.63636364  3.73737374  3.83838384  3.93939394  4.04040404  4.14141414
  4.24242424  4.34343434  4.44444444  4.54545455  4.64646465  4.74747475
  4.84848485  4.94949495  5.05050505  5.15151515  5.25252525  5.35353535
  5.45454545  5.55555556  5.65656566  5.75757576  5.85858586  5.95959596
  6.06060606  6.16161616  6.26262626  6.36363636  6.46464646  6.56565657
  6.66666667  6.76767677  6.86868687  6.96969697  7.07070707  7.17171717
  7.27272727  7.37373737  7.47474747  7.57575758  7.67676768  7.77777778
  7.87878788  7.97979798  8.08080808  8.18181818  8.28282828  8.38383838
  8.48484848  8.58585859  8.68686869  8.78787879  8.88888889  8.98989899
  9.09090909  9.19191919  9.29292929  9.39393939  9.49494949  9.5959596
  9.6969697   9.7979798   9.8989899  10.        ]

You can refer below example, that may work for you but you need to change accordingly.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib import cm
from matplotlib import rc, rcParams
from mpl_toolkits.mplot3d import Axes3D
from scipy.interpolate import griddata
import matplotlib.gridspec as gridspec

df = pd.read_csv('bathy_bedford.csv')
# 2D-arrays from DataFrame
x1 = np.linspace(df['x'].min(), df['x'].max(), len(df['x'].unique()))
y1 = np.linspace(df['y'].min(), df['y'].max(), len(df['y'].unique()))

"""
x, y via meshgrid for vectorized evaluation of
2 scalar/vector fields over 2-D grids, given
one-dimensional coordinate arrays x1, x2,..., xn.
"""

x2, y2 = np.meshgrid(x1, y1)

# Interpolate unstructured D-dimensional data.
z2 = griddata((df['x'], df['y']), df['z'], (x2, y2), method='cubic')

# Ready to plot

fig = plt.figure(211,figsize=(15,20))
ax = fig.add_subplot(211, projection='3d')
spec = gridspec.GridSpec(ncols=1, nrows=2,
                         height_ratios=[4, 1])

surf = ax.plot_surface(x2, y2, z2, rstride=1, cstride=1, cmap=cm.terrain,
                       linewidth=1, antialiased=False)
ax.view_init(45,-55)

cset = ax.contourf(x2, y2, z2, zdir='z2', offset=-80, cmap=cm.terrain, antialiased=True)

rcParams['legend.fontsize'] = 20

rc('text', usetex=True)
rc('axes', linewidth=2)
rc('font', weight='bold')

Here is what i have as plot. 在此处输入图像描述

PS :

But it's not working, it's returning me: "Unknown interpolation method array....".

This is complaining about Interpolate unstructured D-dimensional data . You need to specify the method.

Please refer Documentation Here. z2 = griddata((df['x'], df['y']), df['z'], (x2, y2), method='cubic')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM