简体   繁体   English

如何用 R 生产 3D 分段棒 plot

[英]How to produce 3D segmented bar plot with R

How do we produce a 3D 'segmented' bar plot with R?我们如何使用 R 生产 3D '分段'条 plot? I found a similar post but it was closed many years ago and no working answer has been given till now.我发现了一个类似的帖子,但它在多年前就关闭了,直到现在还没有给出有效的答案。 I could find a solution with python but would really want to generate it with R.我可以用python找到解决方案,但我真的想用 R 生成它。

I understand that some of the bar plots behind will not be clearly presented but I'm not worry about it.我知道后面的一些条形图不会清晰呈现,但我并不担心。 Despite the issue of presenting the data properly in 3D, I still wish to generate it and if possible, rotate so that those behind can be observed at different angle.尽管在 3D 中正确呈现数据存在问题,但我仍然希望生成它,并且如果可能的话,旋转以便可以以不同的角度观察后面的那些。

Can someone help please?有人可以帮忙吗? Thanks.谢谢。

Since I couldn't obtain an answer for using R, I attempted with python (solution by other post but with an increased number of stacks) and achieved what I want, where I can rotate and view the bar chart at different angles.由于我无法获得使用 R 的答案,因此我尝试使用 python(其他帖子的解决方案,但堆栈数量增加)并实现了我想要的,我可以在其中旋转并以不同角度查看条形图。

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


# Set plotting style
plt.style.use('seaborn-white')

dz=[]
z0 = np.array([ 1.,  3.,  11.,   8.,   7.,   6.,   6.,   6.,   5.,   4.,
                3.,   11.,   10.,  1.,  1.,  7.])
dz.append(z0)

z1 =[ 5.,   5.,   8.,   4.,   2.,   0.,   0.,   0.,   0.,   0.,   0.,
      1.,   6.,  5.,   7.,   2.]

dz.append(z1)

z2 =[ 15.,   5.,   8.,   2.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
      3.,   5.,  2.,   7.,   2.]

dz.append(z2)

_zpos = z0*0


xlabels = pd.Index(['X01', 'X02', 'X03', 'X04'], dtype='object')

ylabels = pd.Index(['Y01', 'Y02', 'Y03', 'Y04'], dtype='object')

x = np.arange(xlabels.shape[0])

y = np.arange(ylabels.shape[0])

x_M, y_M = np.meshgrid(x, y, copy=False)

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

# Making the intervals in the axes match with their respective entries
ax.w_xaxis.set_ticks(x + 0.5/2.)
ax.w_yaxis.set_ticks(y + 0.5/2.)

# Renaming the ticks as they were before
ax.w_xaxis.set_ticklabels(xlabels)
ax.w_yaxis.set_ticklabels(ylabels)

# Labeling the 3 dimensions
ax.set_xlabel('X label')
ax.set_ylabel('Y label')
ax.set_zlabel('Z label')

# Choosing the range of values to be extended in the set colormap
values = np.linspace(0.2, 1., x_M.ravel().shape[0])

# Selecting an appropriate colormap

colors = ['#FFC04C', 'blue', '#3e9a19', 
          '#599be5','#bf666f','#a235bf','#848381','#fb90d6','#fb9125']

# Increase the number of segment to 3 by changing the X in 'range(X)' to 3.
for i in range(3):
    ax.bar3d(x_M.ravel(), y_M.ravel(), _zpos, dx=0.3, dy=0.3, dz=dz[i], 
              color=colors[i])
    _zpos += dz[i]
 

#plt.gca().invert_xaxis()
#plt.gca().invert_yaxis()
Segment1_proxy          = plt.Rectangle((0, 0), 1, 1, fc="#FFC04C90")   
Segment2_proxy         = plt.Rectangle((0, 0), 1, 1, fc="blue")

ax.legend([Segment1_proxy,
           Segment2_proxy],['Segment1',
                            'Segment2',
         ])
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM