Skip to content

[ENH]:how to Color 3-D Bars by Height like matlab bar3 function? #23265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
renjf1 opened this issue Jun 14, 2022 · 2 comments
Open

[ENH]:how to Color 3-D Bars by Height like matlab bar3 function? #23265

renjf1 opened this issue Jun 14, 2022 · 2 comments

Comments

@renjf1
Copy link

renjf1 commented Jun 14, 2022

Problem

https://ww2.mathworks.cn/help/matlab/creating_plots/color-3-d-bars-by-height-1.html

how can i draw 3d bars gradient in matplotlib? the bar3d(color, cmap) is not working.

pls help.

Proposed solution

No response

@story645
Copy link
Member

story645 commented Jun 14, 2022

You should be able to pass in a list of colors as with the 2d case, so roughly colors = camp(norm(heights))
ETA: I think I misunderstood you & for the second example, see https://stackoverflow.com/questions/43100082/how-to-plot-gradient-fill-on-the-3d-bars-in-matplotlib

If that doesn't work, please post on discourse.matplotlib.org with more details about what you're doing.

That being said, I'm conflicted on natively supporting cmap for bar since it's unclear which value folks would want to colormap - how to decide if it's height, position, or a third value seperate from either . Though maybe it should be implemented like scatter where it's applied to data that's passed into the color keyword?

@renjf1
Copy link
Author

renjf1 commented Jun 14, 2022

@story645 thks for reply this thread.
test code:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from matplotlib import style
style.use('ggplot')

# setup the figure and axes
fig = plt.figure(figsize=(8, 3))
ax1 = fig.add_subplot(111, projection='3d')

# fake data
_x = np.array([1,2,3,4])
_y = np.array([1,2,3,4,5])
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()

top = x + y
bottom = np.zeros_like(top)
width = depth = 1

norm = plt.Normalize(-1,1)
norm_values = norm(top)
map_vir = plt.cm.get_cmap(name='plasma')
colors = map_vir(norm_values)

ax1.bar3d(x, y, bottom, 0.5, 0.5, top,color=colors,shade=True)
plt.show()

I'm working on a project, like a car, it's has attributes as [speed, press,statistics_value] , the data value will be [20, 3000, 23.6]...... i want to draw a 3d bar to figure the datas. if statistics_value <10 ,the color:'r', if statistics >50, the color:'dark red'. this is my purpose.
Like scatter which give the data to color keyword is simple way, and that's what i want. but 4 lines code in matlab....

I will see the example code, but it's so long....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants