3

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()

The problem: Z-Axis labels do not match discrete values (at -1, 0, 1)

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It works thanks. I totally see what you mean in the second point. But I'm mostly using this as a coding exercise. Somehow I'm failing to set the labels as strings though? Can I not use ax.axes.set_zticklabels(regions[1][:]) to set each tick label? It's printing it everything on top of each other at the minute
@darkace well, what is regions[1][:]?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.