50727

3D plot with categorical axis [Python/Matplotlib]

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.

Recommend

  • Multiple scatterplots using Core Plot and Swift
  • Does Angular assign itself to `window.angular` globally, when loaded as CommonJS module?
  • plotting spatial points over a raster layer in r
  • Einsum optimize fails for basic operation
  • Where to get the .java files of a netbeans project?
  • How to resize image if the image is bigger than Textbox.But it not increase scale if smaller
  • HALF_PTR Windows data type
  • d3.js: why is d3.geo.path() giving NaN?
  • Can XOR be expressed using SKI combinators?
  • F#: In which memory area is the continuation stored: stack or heap?
  • How to merge keras sequential models with same input?
  • Prevent page break in text block with iText, XMLWorker
  • Django foreign key drop down
  • Most efficient way to move table rows from one table to another
  • Can I read an iPhone beacon with Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData
  • Plotting line graph with factors in R
  • xcode don't localize specific strings
  • MS Access - How to change the linked table path by amend the table
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • htaccess add www if not subdomain, if subdomain remove www
  • Swift: Switch statement fallthrough behavior
  • JSON response opens as a file, but I can't access it with JavaScript
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Accessing IRQ description array within a module and displaying action names
  • Join two tables and save into third-sql
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • How to model a transition system with SPIN
  • Convert array of 8 bytes to signed long in C++
  • ORA-29908: missing primary invocation for ancillary operator
  • Numpy divide by zero. Why?
  • KeystoneJS: Relationships in Admin UI not updating
  • trying to dynamically update Highchart column chart but series undefined
  • Understanding cpu registers
  • embed rChart in Markdown
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • Converting MP3 duration time