簡體   English   中英

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

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

我們如何使用 R 生產 3D '分段'條 plot? 我發現了一個類似的帖子,但它在多年前就關閉了,直到現在還沒有給出有效的答案。 我可以用python找到解決方案,但我真的想用 R 生成它。

我知道后面的一些條形圖不會清晰呈現,但我並不擔心。 盡管在 3D 中正確呈現數據存在問題,但我仍然希望生成它,並且如果可能的話,旋轉以便可以以不同的角度觀察后面的那些。

有人可以幫忙嗎? 謝謝。

由於我無法獲得使用 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