Skip to main content

How to plot a 3D bar chart with categorical variable

today I have tried to plot a 3D bar chart with python. For that I used my dataset containing 3 numeric variables and convert 2 in categorical variable, 'fibrinogen' and 'RR__root_mean_square' (by interval; 4 in total) with pd.cut().

An example of the first row of my dataset:

    Fibrinogen  RR__root_mean_square  Weight gain
2         5.15             26.255383         -2.0
3         0.99             20.934106          0.5
7         2.12             24.252434         -1.0
11        1.64             17.004289          3.0
12        3.06             21.201716          0.0

This is my own code :

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# sample data
data = {'Fibrinogen': [5.15, 0.99, 2.12, 1.64, 3.06],
        'RR__root_mean_square': [26.255383, 20.934106, 24.252434, 17.004289, 21.201716],
        'Weight gain': [-2.0, 0.5, -1.0, 3.0, 0.0]}

X_success = pd.DataFrame(data)

x1 = pd.cut(X_success['Fibrinogen'], [0,2,4,10]).cat.codes
y1 = pd.cut(X_success['RR__root_mean_square'], [15,25,32.5,40]).cat.codes
z1 = X_success['Weight gain']

# cat.codes to don't have the 'float() argument must be a string or a number, not 'pandas._libs.interval.Interval'' error
# But I loose the label...

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.bar(x1, y1, z1, zdir='y', alpha=0.8)
plt.show()

With this code I have this unreadable type of graph : enter image description here

In my hope, I would like to have this type of graph : enter image description here

How can I plot a 3D bar chart with python?


This answer is very functional. But, I have some negative value in weight gain (because patient can lose weight). I don't no know if I can modify the color or the visibility for the negative value ? and if can I replace the x and y axes to zero.

enter image description here



source https://stackoverflow.com/questions/76323398/how-to-plot-a-3d-bar-chart-with-categorical-variable

Comments

Popular posts from this blog

ValueError: X has 10 features, but LinearRegression is expecting 1 features as input

So, I am trying to predict the model but its throwing error like it has 10 features but it expacts only 1. So I am confused can anyone help me with it? more importantly its not working for me when my friend runs it. It works perfectly fine dose anyone know the reason about it? cv = KFold(n_splits = 10) all_loss = [] for i in range(9): # 1st for loop over polynomial orders poly_order = i X_train = make_polynomial(x, poly_order) loss_at_order = [] # initiate a set to collect loss for CV for train_index, test_index in cv.split(X_train): print('TRAIN:', train_index, 'TEST:', test_index) X_train_cv, X_test_cv = X_train[train_index], X_test[test_index] t_train_cv, t_test_cv = t[train_index], t[test_index] reg.fit(X_train_cv, t_train_cv) loss_at_order.append(np.mean((t_test_cv - reg.predict(X_test_cv))**2)) # collect loss at fold all_loss.append(np.mean(loss_at_order)) # collect loss at order plt.plot(np.log(al...

Sorting large arrays of big numeric stings

I was solving bigSorting() problem from hackerrank: Consider an array of numeric strings where each string is a positive number with anywhere from to digits. Sort the array's elements in non-decreasing, or ascending order of their integer values and return the sorted array. I know it works as follows: def bigSorting(unsorted): return sorted(unsorted, key=int) But I didnt guess this approach earlier. Initially I tried below: def bigSorting(unsorted): int_unsorted = [int(i) for i in unsorted] int_sorted = sorted(int_unsorted) return [str(i) for i in int_sorted] However, for some of the test cases, it was showing time limit exceeded. Why is it so? PS: I dont know exactly what those test cases were as hacker rank does not reveal all test cases. source https://stackoverflow.com/questions/73007397/sorting-large-arrays-of-big-numeric-stings

How to load Javascript with imported modules?

I am trying to import modules from tensorflowjs, and below is my code. test.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title </head> <body> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script> <script type="module" src="./test.js"></script> </body> </html> test.js import * as tf from "./node_modules/@tensorflow/tfjs"; import {loadGraphModel} from "./node_modules/@tensorflow/tfjs-converter"; const MODEL_URL = './model.json'; const model = await loadGraphModel(MODEL_URL); const cat = document.getElementById('cat'); model.execute(tf.browser.fromPixels(cat)); Besides, I run the server using python -m http.server in my command prompt(Windows 10), and this is the error prompt in the console log of my browser: Failed to loa...