设置具有适当大小和间距的饼图子图

时间:2017-07-25 07:09:45

标签: python python-3.x pandas plotly

我正在努力设置具有适当大小和间距的饼图子图。 如果饼图的大小很小,则数据不可见,如果子图之间的间距不合适,则图形将被填充。 所以子图的布局是以下3行; 2列。 请参阅数据集中的以下代码段:

violent_main=pd.DataFrame({'Counts': crimes[crimes['Class Main Cathegory'].notnull()==True].groupby(['Police District Number'])['Class Main Cathegory'].value_counts()}).reset_index()
violent_main

还附有数据集的打印屏幕: dataset snippet

我正在使用以下代码绘制:

#PD_1D
label_PD_1D=violent_main[violent_main['Police District Number']=='1D']['Class Main Cathegory']
values_PD_1D=violent_main[violent_main['Police District Number']=='1D']['Counts']

#PD_2D
label_PD_2D=violent_main[violent_main['Police District Number']=='2D']['Class Main Cathegory']
values_PD_2D=violent_main[violent_main['Police District Number']=='2D']['Counts']

#PD_3D
label_PD_3D=violent_main[violent_main['Police District Number']=='3D']['Class Main Cathegory']
values_PD_3D=violent_main[violent_main['Police District Number']=='3D']['Counts']

#PD_4D
label_PD_4D=violent_main[violent_main['Police District Number']=='4D']['Class Main Cathegory']
values_PD_4D=violent_main[violent_main['Police District Number']=='4D']['Counts']

fig = {
  "data": [
    {
      "values": values_PD_1D,
      "labels": label_PD_1D,
      "domain": {'x': [0.0, 0.35], 'y': [2.22, 2.53]},
      "name": "PD_1D",
      "hoverinfo":"label+percent+name",
      "hole": .4,  
      "type": "pie"
    },     
    {
      "values": values_PD_2D,
      "labels": label_PD_2D,
      "text":"CO2",
      "textposition":"inside",
      "domain":{'x': [0.50, 0.85], 'y': [2.22, 2.53]},
      "name": "PD_2D",
      "hoverinfo":"label+percent+name",
      "hole": .4,  
      "type": "pie"
    },
     {
      "values": values_PD_3D,
      "labels": label_PD_3D,
      "text":"CO2",
      "textposition":"inside",
      'domain': {'x': [0.0, 0.35], 'y': [.9, 1.13]},
      "name": "CO2 Emissions",
      "hoverinfo":"label+percent+name",
      "hole": .4,   
      "type": "pie"
    }  
  ],
  "layout": {
        "title":"Violent Crimes by main cathegory",
        "annotations": [
            {
                "font": {
                    "size": 20
                },
                "showarrow": False,
                "text": "PD_1D",
                "x": 0.12,
                "y": 0.5
            },
            {
                "font": {
                    "size": 20
                },
                "showarrow": False,
                "text": "PD_2D",
                "x": 0.73,
                "y": 0.5
                },
            {
                "font": {
                    "size": 20
                },
                "showarrow": False,
                "text": "PD_3D",
                "x": 0.20,
                "y": 0.25
                }
            ]
        }
    }
iplot(fig, filename='donut')

在这段代码中我试图使用“域”来操纵布局:{x:[],y:[]) 但是我无法进行所需的布局。因此,例如,地块编号3较小,高于地块1和地图2.我想要的是该地块与其他地块的大小相同但是低于 请看下图: plot

我希望我能找出逻辑,以便绘制饼图:3行:2列。 请指教

2 个答案:

答案 0 :(得分:0)

看看这个优秀的答案:How to plot pie charts as subplots with custom size with Plotly in Python

为了获得相同大小的图,他们的domain跨度需要相同。域名从底部开始(例如0表示已关闭,1表示已启动)。

enter image description here     values_PD_1D = [i for i in range(30,100,10)]     values_PD_2D = values_PD_1D [:: - 1]     values_PD_3D = values_PD_1D [:: - 2]

label_PD_1D = ['Javascript', 'Python', 'R', 'Java', 'C#', 'C++', 'Perl']
label_PD_2D = label_PD_1D
label_PD_3D = label_PD_1D[::-2]

fig = {
  "data": [
    {
      "values": values_PD_1D,
      "labels": label_PD_1D,
      "domain": {'x': [0.0, 0.45], 'y': [0, 0.45]},
      "name": "PD_1D",
      "hoverinfo":"label+percent+name",
      "hole": .4,  
      "type": "pie"
    },     
    {
      "values": values_PD_2D,
      "labels": label_PD_2D,
      "textposition":"inside",
      "domain":{'x': [0.55, 1], 'y': [0, 0.45]},
      "name": "PD_2D",
      "hoverinfo":"label+percent+name",
      "hole": .4,  
      "type": "pie"
    },
     {
      "values": values_PD_3D,
      "labels": label_PD_3D,
      'domain': {'x': [0.0, 0.45], 'y': [0.55, 1]},
      "hoverinfo":"label+percent+name",
      "hole": .4,   
      "type": "pie"
    }  
  ],
  "layout": {
        "title":"Programming languages",
        "annotations": [
            {
                "font": {
                    "size": 20
                },
                "showarrow": False,
                "text": "PD_1D",
                "x": 0.12,
                "y": 0.5
            },
            {
                "font": {
                    "size": 20
                },
                "showarrow": False,
                "text": "PD_2D",
                "x": 0.73,
                "y": 0.5
                },
            {
                "font": {
                    "size": 20
                },
                "showarrow": False,
                "text": "PD_3D",
                "x": 0.12,
                "y": 1.1
                }
            ]
        }
    }

plotly.offline.iplot(fig, filename='donut')

答案 1 :(得分:0)

使用像这样的布局字典可以实现我想要的东西:

layout = dict(height = 1200,
              width = 1000,
              autosize = False,
              title = 'Violent Crimes/PD by main cathegory',
              annotations= [{"font": {"size": 24},"showarrow": False,"text": "PD_1D","x": 0.14, "y": 0.82},
                            {"font": {"size": 24},"showarrow": False,"text": "PD_2D","x": 0.86, "y": 0.82},
                            {"font": {"size": 24},"showarrow": False,"text": "PD_3D","x": 0.14, "y": 0.48},
                            {"font": {"size": 24},"showarrow": False,"text": "PD_4D","x": 0.86, "y": 0.48},
                            {"font": {"size": 24},"showarrow": False,"text": "PD_5D","x": 0.14, "y": 0.13},
                            {"font": {"size": 24},"showarrow": False,"text": "PD_6D","x": 0.86, "y": 0.13}]

              )

在布局字典中,width,height参数控制子图的大小。 x,y域为每个子图提供更多的起点,并且绑定到1的上限。因此,您只能使用域来获得良好的细节级别和子图之间的间距。 请参阅随附的打印屏幕 Pie chart with appropriate size

这可以关闭。 非常感谢您的帮助