Tracciamento di punti dati a 3 tuple in un grafico di superficie/contorno utilizzando matplotlib
-
26-09-2019 - |
Domanda
Ho alcuni dati di superficie generati da un programma esterno come valori XYZ.Voglio creare i seguenti grafici, utilizzando matplotlib:
- Trama di superficie
- Trama di contorno
- Trama di contorno sovrapposta a una trama di superficie
Ho esaminato diversi esempi per tracciare superfici e contorni in matplotlib, tuttavia, i valori Z sembrano essere una funzione di X e Y, ad es.Y ~ f(X,Y).
Presumo che avrò in qualche modo bisogno di trasformare le mie variabili Y, ma non ho ancora visto alcun esempio che mostri come farlo.
Quindi, la mia domanda è questa:dato un insieme di punti (X, Y, Z), come posso generare grafici di superficie e di contorno da tali dati?
A proposito, giusto per chiarire, NON voglio creare grafici a dispersione.Inoltre, anche se ho menzionato matplotlib nel titolo, non sono contrario all'uso di rpy(2), se ciò mi consentirà di creare questi grafici.
Soluzione
per fare un trama di contorno è necessario interpolare i dati ad una griglia regolare http://www.scipy.org/Cookbook/Matplotlib/Gridding_irregularly_spaced_data
un esempio veloce:
>>> xi = linspace(min(X), max(X))
>>> yi = linspace(min(Y), max(Y))
>>> zi = griddata(X, Y, Z, xi, yi)
>>> contour(xi, yi, zi)
per il superficie http: //matplotlib.sourceforge. net / examples / mplot3d / surface3d_demo.html
>>> from mpl_toolkits.mplot3d import Axes3D
>>> fig = figure()
>>> ax = Axes3D(fig)
>>> xim, yim = meshgrid(xi, yi)
>>> ax.plot_surface(xim, yim, zi)
>>> show()
>>> help(meshgrid(x, y))
Return coordinate matrices from two coordinate vectors.
[...]
Examples
--------
>>> X, Y = np.meshgrid([1,2,3], [4,5,6,7])
>>> X
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
>>> Y
array([[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7]])
profilo in 3D http: //matplotlib.sourceforge. net / examples / mplot3d / contour3d_demo.html
>>> fig = figure()
>>> ax = Axes3D(fig)
>>> ax.contour(xi, yi, zi) # ax.contourf for filled contours
>>> show()
Altri suggerimenti
Con panda e numpy per importare e manipolare i dati, con matplot.pylot.contourf per tracciare l'immagine
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata
PATH='/YOUR/CSV/FILE'
df=pd.read_csv(PATH)
#Get the original data
x=df['COLUMNNE']
y=df['COLUMNTWO']
z=df['COLUMNTHREE']
#Through the unstructured data get the structured data by interpolation
xi = np.linspace(x.min()-1, x.max()+1, 100)
yi = np.linspace(y.min()-1, y.max()+1, 100)
zi = griddata(x, y, z, xi, yi, interp='linear')
#Plot the contour mapping and edit the parameter setting according to your data (http://matplotlib.org/api/pyplot_api.html?highlight=contourf#matplotlib.pyplot.contourf)
CS = plt.contourf(xi, yi, zi, 5, levels=[0,50,100,1000],colors=['b','y','r'],vmax=abs(zi).max(), vmin=-abs(zi).max())
plt.colorbar()
#Save the mapping and save the image
plt.savefig('/PATH/OF/IMAGE.png')
plt.show()
plot Contour con rpy2 + ggplot2:
from rpy2.robjects.lib.ggplot2 import ggplot, aes_string, geom_contour
from rpy2.robjects.vectors import DataFrame
# Assume that data are in a .csv file with three columns X,Y,and Z
# read data from the file
dataf = DataFrame.from_csv('mydata.csv')
p = ggplot(dataf) + \
geom_contour(aes_string(x = 'X', y = 'Y', z = 'Z'))
p.plot()
trama superficiale con rpy2 + reticolo:
from rpy2.robjects.packages import importr
from rpy2.robjects.vectors import DataFrame
from rpy2.robjects import Formula
lattice = importr('lattice')
rprint = robjects.globalenv.get("print")
# Assume that data are in a .csv file with three columns X,Y,and Z
# read data from the file
dataf = DataFrame.from_csv('mydata.csv')
p = lattice.wireframe(Formula('Z ~ X * Y'), shade = True, data = dataf)
rprint(p)