Color map for 3d bar plot where the color is with respect to an independent value

135 views Asked by At

I want to have my color bar change with each element and represent the Ionization # in the data set below:

   Ionization #       Name  Nebulizer 1  Nebulizer 2  Nebulizer 3
0         4.341  Potassium      27.0923      18.5475      25.2555
1         5.139     Sodium      23.1222      21.1542      20.5644
2         5.212     Barium       0.1975       0.0721       0.1665
3         5.986  Aluminium       5.0517       3.9544       0.3125
4         6.108    Thalium       0.1484       0.3125       0.0154

enter image description here

The colour map should give elements with a lower ionization potential a red shift and gives elements with higher IP's a blue shift like this colour bar below: enter image description here

I have found questions where they do this in 2d but I have had trouble doing it in 3d. I tried adjusting the code given in This stack overflow question but haven't had much success. Here is what I have right now, its horrible and ugly, and doesn't work. I haven't been able to get the rgba to get individual values for each element. This code just makes every bar the same colour.

macro_df = df.loc[df['Nebulizer 1'] + df['Nebulizer 2'] + df['Nebulizer 3'] >= 3]
ion_grid = []
ion_num = macro_df['Ionization #']
for i in ion_num:
    ion_grid.append(i)
    ion_grid.append(i)
    ion_grid.append(i)
col = cm.jet(np.random.rand(ion_grid.shape[0], ion_grid.shape[1]))
ax1.bar3d(xpos_mac, ypos_mac, zpos_mac, dx, dy, dz_macro, color=rgba)

Lastly, I wanted to include the reproducible data frame I've been using for this question.

df = pd.DataFrame({'Ionization #': [4.341, 5.139, 5.212, 5.986, 6.108],
                   'Name': ['Potassium', 'Sodium', 'Barium', 'Aluminium', 'Thalium'],
                   'Nebulizer 1': [27.0923, 23.1222, 0.1975, 5.0517, 0.1484],
                   'Nebulizer 2': [18.5475, 21.1542, 0.0721, 3.9544, 0.3125],
                   'Nebulizer 3': [25.2555, 20.5644, 0.1665, 0.3125, 0.0154]})

I can provide my code if that is helpful

1

There are 1 answers

0
Julian Avila On

This link was where I found how to find my data's colour. I got rid of the 'col' variable line and replaced that line with

rgba_macro = [cmap((k-3.89)/24.59) for k in ion_grid_macro]

Below is the code for the colour bar:

sm = ScalarMappable(cmap=my_cmap, norm=plt.Normalize(min, maxx))
sm.set_array([])
cbar = plt.colorbar(sm, shrink=0.5)
cbar.set_label('Ionization Potential (eV)', rotation=270, labelpad=15)