簡體   English   中英

DataFrame:如何使用索引和列作為 x 和 y,數據作為 z 來繪制 3D 圖?

[英]DataFrame: how to draw a 3D graph using Index and Columns as x and y, and data as z?

我的 DataFrame 行索引和列索引存儲xy值,而數據值是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM