[英]Convert pandas DataFrame to a 3d graph using Index and Columns as X,Y and values as Z?
[英]DataFrame: how to draw a 3D graph using Index and Columns as x and y, and data as z?
我的 DataFrame 行索引和列索引存儲x
和y
值,而數據值是z
值,即f(x, y)
。
舉個例子:
import pandas as pd
df = pd.DataFrame([[150, 120, 170], [190, 160, 130]],
index=[2, 4], columns=[10, 30, 70])
print(df)
# 10 30 70
# 2 150 120 170
# 4 190 160 130
那么f(4, 30)
是160
。
我想做一個 3D plot 的 function f
。 我真的不介意它看起來像 3D 直方圖還是表面 plot - 這兩個答案都將不勝感激。
您需要從您的行和列索引創建一個網格網plot_surface
meshgrid
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame([[150, 120, 170], [190, 160, 130]],
index=[2, 4], columns=[10, 30, 70])
x,y = np.meshgrid(df.columns, df.index)
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(x, y, df)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.