简体   繁体   English

Matlab:具有6 * 12矩阵颜色代码的3D饼图

[英]Matlab: 3D Pie chart with color code from 6*12 matrix

I was wondering if it was possible to color code a pie chart created on matlab with a different set of data than the one used to create the pie chart. 我想知道是否可以对在matlab上创建的饼图进行颜色编码,而使用的数据集与用于创建饼图的数据集不同。

This chart is based on this code: 该图表基于以下代码:

for F=1:6
    labels= {'Segment 1', 'Segment 2', 'Segment 3', 'Segment 4', 'Segment 5', 'Segment 6',         'Segment 7', 'Segment 8', 'Segment 9', 'Segment 10', 'Segment 11', 'Segment 12'};
    figure;
    x= [1 1 1 1 1 1 1 1 1 1 1 1];
    pie3(x,labels);
    colormap gray;
    title({'Floor n°=Figure n°';'Floor height=83.33mm';'Segment angle=30'});
end

What I want to do is to use a color code to assign values in the range 500 - 5000 from a 6*12 matrix to each segment of the pie chart(there are 6 of these figures, each showing 12 segments). 我想做的是使用一个颜色代码将一个6 * 12矩阵的值在500-5000范围内分配给饼图的每个部分(这些图有6个,每个显示12个部分)。 Currently, the colormap has a range from 1-12 and gives a different color to each segment. 当前,颜色图的范围是1到12,并且为每个段赋予不同的颜色。 I need the pie chart to remain the same as it represents a column divided into segments of equal volume. 我需要饼图保持不变,因为它代表一列,分成相等数量的段。 I am not a matlab expert but I was wondering if this was possible without having to create a pie chart from scratch, thus using the pie3 function. 我不是matlab专家,但我想知道是否有可能不必从头开始创建饼图,从而使用pie3函数。

Thanks in advance. 提前致谢。

You definitely can color the pie charts (and 3D pie charts) with any color you like, and they do not need to be tied to the data initially used to create the pie chart. 您绝对可以使用任意颜色为饼图(和3D饼图)着色,并且它们不必与最初用于创建饼图的数据绑定。

Based on your explanations, I am not certain of what color you want to apply or how you are going to determine the color of a given segment of your pie, but I can give you an easy way to apply any color to a segment of a pie chart. 根据您的解释,我不确定您要应用哪种颜色或如何确定馅饼的给定段的颜色,但是我可以为您提供一种将任意颜色应用于段的简单方法饼形图。 This way you can decompose your problem into 3 steps: 这样,您可以将问题分解为3个步骤:

  • Generate your generic pie charts ( already done in your example code ) 生成通用饼图( 已在示例代码中完成
  • calculate the colors of the segments, based on your algorithm 根据您的算法计算段的颜色
  • Apply these color to the relevant segments of your pie charts ( where I come in ) 将这些颜色应用于饼图的相关部分( 我来的地方

How it works: 这个怎么运作:

When you generate a 3D pie chart, Matlab first calculate the proportion to give to each segment ( all equals in your case ), then generates the graphic output. 生成3D饼图时,Matlab首先计算要分配给每个段的比例( 在您的情况下均等于 ),然后生成图形输出。 For that matlab generates four graphic objects per segment of the pie chart: 为此,matlab在饼图的每个分段上生成四个图形对象

  • 3 patch objects ( used to display the top, bottom and side of the given segment ) 3个补丁对象( 用于显示给定段的顶部,底部和侧面
  • 1 text object ( used for the text label of the segment ) 1个文本对象( 用于段的文本标签

The solution will simply consists in retrieving the handles of the graphic objects in order to assign a color to them. 该解决方案将仅在于检索图形对象的句柄,以便为其分配颜色。
Since in your case the collection of handle will be significant, we will also re-arrange it slightly to allow an easy allocation of color for a given segment, which will look like: 由于在您的情况下,手柄的集合非常重要,因此我们也将对其进行稍微重新排列,以使给定段的颜色易于分配,如下所示:

set( HandleCollection( FigureNumber , SegmentNumber) , desiredColor )

First, I add to modify slightly your example, because we need to retrieve the handles of the graphic objects at he time they are created (much easier this way). 首先,我要稍微修改一下您的示例,因为我们需要在创建图形对象时检索它们的句柄(这种方法要容易得多)。 So here it is: 所以这里是:

x= [1 1 1 1 1 1 1 1 1 1 1 1] ;

nPieChart = 3 ;         %// only 3 figures in this example, but any number can work
nSegments = length(x) ; %// number of segment for each pie chart

hPie = zeros( nSegments*4 , nPieChart ) ; %// initialise the handle matrix

% // Create your pie charts
for F=1:nPieChart
    labels= {'Segment 1', 'Segment 2', 'Segment 3', 'Segment 4', 'Segment 5', 'Segment 6',         'Segment 7', 'Segment 8', 'Segment 9', 'Segment 10', 'Segment 11', 'Segment 12'};
    figure;
    hPie(:,F) = pie3(x,labels) ;
    colormap gray;
    title({['Floor n°=' num2str(F)];'Floor height=83.33mm';'Segment angle=30'});
end

I took the definition of x out of the loop so I was able to pre-assign the size of the matrix hPie which will contain the handles (and also because if x never changes, no need to re-calculate it at every loop iteration. ( By the way, the same could apply to the labels if they do not change from one figure to another ). 我将x的定义排除在了循环之外,因此我能够预先分配将包含句柄的矩阵hPie的大小(并且还因为如果x永不改变,则无需在每次循环迭代时重新计算它。 ( 顺便说一句,如果labels没有从一个数字更改为另一个数字,则同样适用于labels )。

Now we have a good collection of handle, let's reorder them in a more convenient manner. 现在我们有了一个好的句柄集合,让我们以更方便的方式对其进行重新排序。 First we extract all the handles of the text labels ( we want these separated because they have different properties than the patch objects ): 首先,我们提取文本标签的所有句柄( 我们希望将它们分开,因为它们的属性与补丁对象不同 ):

idx_textHandles = 4:4:nSegments*4 ;
hLabels = hPie( idx_textHandles , : ).' ;

The last .' 最后一个.' operator is used to transpose the matrix so we can acces the hLabels table by ( figureNumber , segmentNumber ). 运算符用于转置矩阵,因此我们可以通过( FigureNumbersegmentNumberhLabels表。 It seemed more intuitive to me to address the figure number before the segment number in an assignation . 对我来说,在分配中在段号之前解决图形编号似乎更直观

Next we strip the hPie matrix of the text handles we just saved, then we reshape so the dimensions will be (m,n,p), with: 接下来,我们剥离刚刚保存的文本句柄的hPie矩阵,然后重新hPie ,使尺寸为(m,n,p),其中:

m = The number of figures m =数字
n = the number of segments in each pie chart n =每个饼图中的段数
p = 3 (the 3 handles of the patch objects defining a segment) p = 3(补丁对象的3个句柄定义一个段)

hPie( idx_textHandles , : ) = [] ;
hSegment = permute( reshape( hPie , 3 , nSegments , nPieChart   ) , [3 2 1] ) ;

That's it ! 而已 ! You can now assign a color to a given segment with only one line of code, by setting the 'FaceColor' property of a patch object. 现在,您可以通过设置补丁对象的'FaceColor'属性,仅用一行代码将颜色分配给给定的段。 For example the instruction: 例如,指令:

set( hSegment( 2 , 5 , : ) , 'FaceColor','r' )

红色部分

will color the segment #5 of the figure #2 in red. 将以红色将图#2的第5段着色。 You can use any predefined color or the usual [RVB] triplets. 您可以使用任何预定义的颜色或常规的[RVB]三胞胎。 You can also set the text of a given segment. 您还可以设置给定句段的文本。 So: 所以:

set( hLabels( 3 , 2) , 'String','HELLO')
set( hSegment( 3 , 2 , : ) , 'FaceColor', [.75 .75 .75] )

will color the segment #2 of the figure #3 in a light gray, and will set it's text label to 'HELLO'. 将以浅灰色将图#3的段#2着色,并将其文本标签设置为“ HELLO”。


mmmh wait ! 嗯,等等! If you are keyboard lazy or simply if like me you are bothered with this type of matrix assignment (nFig, Nsegment, :) .After all, the last dimension of the matrix will always have to be assigned in full if we want to color the full segment, so having to specify the : every time is annoying ... No problem ... one more line of code and things will be even easier in the future: 如果您不喜欢键盘,或者像我一样简单,那么您就会为这种矩阵分配感到烦恼(nFig, Nsegment, :) 。毕竟,如果我们想给矩阵(nFig, Nsegment, :)矩阵的最后一个维度将总是必须被完全分配。全段,因此必须指定:每次都很烦人...没问题...再增加一行代码,将来事情会变得更加容易:

hdlSegments = num2cell( hSegment, [nSegments  nPieChart] ) ;

Cool, we got rid of these trailing : in our assignments, now we can simply assign a color specifying the figure number then segment number. 太酷了,我们摆脱了这些尾随:在分配中,现在我们可以简单地分配一种颜色,指定图号然后是段号。 For example: 例如:

set( hdlSegments{ 3 , 6 } , 'FaceColor','m')

will set a nice magenta to the faces of the segment #6 of the figure #3. 将在图#3的段#6的脸上设置好洋红色。

Just pay attention now we have to use the {} instead of the () because we are accessing a cell array and not a simple numeric array anymore. 请注意,现在我们必须使用{}而不是(),因为我们正在访问单元格数组,而不是简单的数字数组。


Not short enough ? 还不够短? extremely keyboard lazy ? 键盘极其懒惰? ... ok last tip to reduce the syntax even more. ...好的最后一个技巧是减少语法。 One easy way would be to write a function to assign the 'facecolor' property of the 3 patch objects, but the code is so short it is almost a waste of a new file ... you can do it in one line: 一种简单的方法是编写一个函数来分配3个补丁对象的'facecolor'属性,但是代码太短了,几乎浪费了一个新文件……您可以一行完成:

colorSegment = @(fig,seg,color) set( hdlSegments{fig,seg} ,'FaceColor',color)

youhou, now you can type: youhou,现在您可以输入:

colorSegment( 3 , 4 , [0 0 1] )

and see the segment #4 of figure #3 change to a nice blue. 并看到图3的段4变成了漂亮的蓝色。 Of course, if you did that because you are keyboard lazy you can give a shorter name to the function . 当然,如果您这样做是因为您懒于键盘操作,则可以为该函数指定一个较短的名称

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

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