无法使用图例对数据框进行3d绘制 [英] Unable to make 3d Plots with legend for dataframes

查看:55
本文介绍了无法使用图例对数据框进行3d绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Pandas.DataFrame 对象制作3d图.

I am trying to make a 3d plot from a Pandas.DataFrame object.

要求

  • 要为 z 绘制的列数可能会有所不同,因此我对具有固定 x z 值使用了循环和 y 值.该代码显示在代码1 中.
  • The number of columns to be plotted for z may vary and hence I am using a loop for the z values with a fixed x and y values. The code is shown in Code 1.

代码1

import matplotlib.pyplot as plt
import urllib, base64
from mpl_toolkits.mplot3d import axes3d
import numpy as np
import pandas as pd

column_names = ['A', 'B', 'C', 'D', 'E']
df = pd.DataFrame(columns=column_names)

fig2 = plt.figure(figsize=(15,15))
ax2 = fig2.add_subplot(111, projection='3d')

for x in df.columns:
    if(x!='A' and x!='B'):
      ax2.plot_surface(df['A'].values, df['B'].values, df[x].values, linewidth=0, antialiased=False)
      ax2.legend()

问题:

当我执行 Code 1 时,出现错误-

When I execute Code 1, I get an error -

Argument Z must be 2-dimensional. 

使用 plot_trisurf 时,我已经解决了问题,如 Code 2 所示.

I have solved it when i used - plot_trisurf, as shown in Code 2.

代码2

for x in df.columns:
    if(x!='A' and x!='B'):
      ax2.plot_trisurf(df['A'].values, df['B'].values, df[x].values, linewidth=0, antialiased=False)
      ax2.legend()

但是现在我遇到了另一个错误-

But now I am getting a different error -

Error in qhull Delaunay triangulation calculation: singular input data (exitcode=2); use python verbose option (-v) to see original qhull error.

问题

  • 如何为 Pandas.DataFrame 绘制3d图,并为Z with Legend
  • 的列数不同
  • How can I make 3d plots for a Pandas.DataFrame with different number of columns for Z with Legend

注意

上面提供的数据仅用于实验,可能不统一,可以有小数点.

The data provided above is just for experimentation and may not be uniform and can have decimals.

推荐答案

您遇到的qhull错误可能是因为您的'A''B'列包含线性相关的数据,即,点在几何上位于一条线上(您没有发布数据,所以我无法验证这一点). plot_trisurf 函数尝试构建 Qhull 调用错误,Qhull是用于构造Delaunay的基础库三角剖分(另请参见我的回答在这里).

The qhull error you're getting is probably because your 'A' and 'B' columns contain data that is linearly dependent, i.e., the points are geometrically on a line (you did not post the data so I cannot verify this). The plot_trisurf function tries to construct a Delaunay triangulation from the X, Y parameters you pass it (the df['A'].values, df['B'].values in your code). A singular/degenerate configuration invokes the error from Qhull, which is the underlying library that is used to construct the Delaunay triangulation (see also my answer here).

如果数据是奇异/简并的,则可以使用散点图线图.

If your data is singular/degenerate you can use scatter plots or line plots instead.

如果您坚持使用表面图,并且数据是奇异的,则可以尝试 X,Y 数据,因此基础三角剖分不会失败.

If you insist on a surface plot, and your data is singular, you might try to "joggle" the X, Y data so the underlying triangulation will not fail.

这篇关于无法使用图例对数据框进行3d绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆