views:

624

answers:

4

Hello!

How to add strings to the axes in Axes3D instead of numbers?

I just started using the matplotlib. I have used Axes3dD to plot similar to the example given on their website (http://matplotlib.sourceforge.net/examples/mplot3d/bars3d%5Fdemo.html). Note that one must use the last verson (matplotlib 0.99.1), otherwise the axis gets a bit freaky. The example use this code:

from mpl_toolkits.mplot3d import Axes3D  
import matplotlib.pyplot as plt  
import numpy as np  

fig = plt.figure()  
ax = Axes3D(fig)  
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
    xs = np.arange(20)
    ys = np.random.rand(20)
    ax.bar(xs, ys, zs=z, zdir='y', color=c, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

Now to my problem, I cant rename the axis to what I want. Instead of numbers i need to name the staples to string names. This I only want to do in two dimensions - the x dimension, and the z dimension (depth). I have tried to use this command:

ax.set_xticklabels('First staple',(0,0))

I get no errormessage but nevertheless no sting. If anyone can answer this question I would be most delighted!

Cheers Andreas

A: 

Your actually on the right path there. but instead of a string you will want to pass a list or tuple to set_xticklabels(). You may also wish to adjust the center location for the label with set_xticks().

You may also find this function of use get_xmajorticklabels(). It will return the rendered tick labels. So you may also be able to adjust the value from its results.

NerdyNick
A: 

Also retrieve the axis data, work on that and set it back to the renderer, turned out to be much simpler for me ;) other than that, what NerdyNick says

Jokey
A: 

Hello All,

I'm currently in the same situation as Andreas. I am trying to set labels to my axe ticks on both the x axis and z axis of a 3D plot. And I am not able to find the good way to achieve it.

here my script:


Batiments = [batA,batB,batC,batD,batE]
ax = Axes3D(fig)  #
for c, z, bat in zip(['grey', 'orange', 'cyan', 'green', 'red'], [4, 3, 2, 1, 0], Batiments):
    xs = arange(11)
    ys = Pabs[bat["leg"]]["Ptot"]["D"]
    ax.bar(xs, ys, zs=z, zdir='y', color=c, alpha=0.7)
"""TODO: set the Leg_x's labels under my x axis ticks"""
Leg_x = ["Ouest_0","Ouest_1","Ouest_2","Ouest_3","Ouest_4",
         "Toiture","Est_0","Est_1","Est_2","Est_3","Est_4"]
print(ax.get_xlim3d())# display [ 328.64864865  37690.81081081]
ax.set_xticks(range(0,38000,3600))
ax.set_xticklabels(Leg_x)
"""TODO: set the Leg_z's labels under my z axis ticks"""
Leg_z = ["A","B","C","D","E"]
print(ax.get_zlim3d())# display [ 0.  4.]
ax.set_zticks(range(5))
ax.set_zticklabels(Leg_z)

ax.set_xlabel(u'Emplacement facette')
ax.set_ylabel(u'Bâtiments')
ax.set_zlabel(u'Puissance solaire totale absorbée [W]')

A first problem happens with my "supposed to be" yticks label is displayed on the x axis. Moreover the adjustment just does not work (the zticks... functions are display here as examples, but of course those don't work at all because there are not in Matplotlib) , Anyone has an idea to resolve my problems?

Best regards, Max

Max
A: 

Hello Jokey,

Thanks for ur help... though I can't make it work.

To assign the current xticks_label to the axis' yticks I've tried to use "juggle_axes(xs, ys, zs, zdir)" without success...

And which matplotlib function do you preconize to retrieve the 3daxis data?

I've tried "get_yticks" but the answer is definitively an empty tuple, and as soon I am trying to set_yticks with a "linspace (ylim3d[0], ylim3d[1], N)" in order to asign my own ticklabels later, the Axes3d object shrinks (you can only perceive axis labels) and becomes impossible to handle.

I am a new matplotlib user, The functions and classes associated with mpl_toolkits.mplot3d.axes3d are not really clear to me. Maybe you could tell me what to use to retrieve the axis data and then to set it back to the renderer...?

Thanks you in advance for your help max

Max