具有分类轴的3D图[Python/Matplotlib] [英] 3D plot with categorical axis [Python/Matplotlib]

查看:44
本文介绍了具有分类轴的3D图[Python/Matplotlib]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试绘制具有以下特征的三维图形:

Trying to plot a 3 dimensional graph with:

x 轴 - 值(浮动)y轴-值(浮点)z轴-类别(字符串)

x axis - Values (float) y axis - Values (float) z axis - Category (string)

现在我已经尝试使用 pandas.factorize(table.zcolumn) 来转换 z 向量

Now I've tried to convert the z vector using pandas.factorize(table.zcolumn)

Output: (array([ 0,  0,  0, ..., -1, -1,  1]), Index([u'London', u'National'], dtype='object'))

所以我可以画出数字没问题.

So I can plot the numbers no problem.

您会看到有 NaN 值会转换为 -1,因此当我绘制图形时,有一堆值为 -1 的值.数据包含伦敦、国家和 NaN 类别.

You'll see there are NaN values which convert to -1, so when I plot the graph there are a bunch of values at -1. The data hold London, National and NaN categories.

如何标记坐标轴以适合我的数据?我觉得应该有一个简单的函数可以匹配它.

How can I label the axes to fit my data? I feel like there should be a simple function to match it.

在 z 轴上,我需要重新分配刻度 -1 以变为NA",将 0 变为伦敦",将 1 变为全国"

On the z-axis I need to reassign the ticks -1 to become 'NA', 0 to become 'London' and 1 to become 'National'

我也对使用大量类别执行此操作的方法感兴趣,因此无需手动输入每个类别字符串的代码

I'd also be interested in a way for doing this with large numbers of categories, so code that does not need manually inputting each category string

regions = pandas.factorize(dataTable.Region[id_range])
regions_num = regions[0]

fig = plot.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(y, x, zs=regions_num)

ax.axes.set_zticklabels(["London","National","N/A"])

plot.show()

推荐答案

您只需将 zticks 设置为与您的类别相对应的三个z值:

You just need to set the zticks to the three z-values corresponding to your categories:

ax.axes.set_zticks(regions_num)

话虽如此,但我并不是说这实际上是绘制数据的一种很好的方法.当您的X,Y和Z值都是连续变量时,3D图最有用.如果区域"是一个序数变量,则将区域表示为不同的z级别可能会更有意义,但是出于任何原因,为什么'N/A'应该比'code>'更高'国家/地区的?3D图通常也比2D图更难读-例如,由于透视投影,在'National'类别中的一个点可能看起来很像一个更远的点,但在'N/A' 类别.

Having said that, I don't this is actually a very good way of plotting your data. 3D plots are most useful when your X, Y and Z values are all continuous variables. Representing regions as different z-levels might make a bit more sense if 'region' was an ordinal variable, but is there any reason why 'N/A' should be 'higher' than 'National'? 3D plots are also generally harder to read than 2D plots - for example, because of the perspective projection a point that's nearby in the 'National' category might look a lot like a point that's further away but in the 'N/A' category.

一个更合适的选择可能是将这些数据表示为二维坐标轴上的散点图,并用不同的颜色表示不同的类别.

A more appropriate choice might be to represent these data as a scatter plot on 2D axes, with different colours corresponding to the different categories.

这篇关于具有分类轴的3D图[Python/Matplotlib]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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