![3D plot with categorical axis [Python/Matplotlib]](https://www.xszz.org/skin/wt/rpic/t14.jpg)
Question:
Trying to plot a 3 dimensional graph with:
x axis - Values (float) y axis - Values (float) z axis - Category (string)
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.
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.
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()
<img alt="The problem: Z-Axis labels do not match discrete values (at -1, 0, 1)" class="b-lazy" data-src="https://i.stack.imgur.com/UVWiB.png" data-original="https://i.stack.imgur.com/UVWiB.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />
Answer1:You just need to set the zticks
to the three z-values corresponding to your categories:
ax.axes.set_zticks(regions_num)
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.