简体   繁体   English

使用domain属性并排绘制3个饼图

[英]Plotting 3 pie charts side-by-side using the domain property

I am trying to plot 3 pie charts side by side. 我正在尝试并排绘制3个饼图。 I don't understand why the following code is making the pie charts go across the page diagonally left to write rather than horizontally left to write in one line. 我不明白为什么下面的代码使饼形图跨页面斜向左写而不是水平向左写一行。

Here's my code: 这是我的代码:

app.layout = html.Div([
    html.Div([
        dcc.Graph(id='TPiePlot',
                  figure={
                      'data': [go.Pie(labels=labels1,
                                      values=values1,
                                      marker=dict(colors=colors, line=dict(color='#fff', width=1)),
                                      hoverinfo='label+value+percent', textinfo='value',
                                      domain={'x': [0, .25], 'y': [0, 1]}
                                      )
                               ],
                      'layout': go.Layout(title='T',
                                          autosize=True
                                          )
                  }
                  ),
        dcc.Graph(id='RPiePlot',
                  figure={
                      'data': [go.Pie(labels=labels2,
                                      values=values2,
                                      marker=dict(colors=colors, line=dict(color='#fff', width=1)),
                                      hoverinfo='label+value+percent', textinfo='value',
                                      domain={'x': [0.30, .55], 'y': [0, 1]}
                                      )
                               ],
                      'layout': go.Layout(title='R',
                                          autosize=True
                                          )
                  }
                  ),

        dcc.Graph(id='RoPiePlot', 
                  figure={
                      'data': [go.Pie(labels=labels3,
                                      values=values3,
                                      marker=dict(colors=colors, line=dict(color='#fff', width=1)),
                                      hoverinfo='label+value+percent', textinfo='value',
                                      domain={'x': [0.60, 0.85], 'y': [0, 1]}
                                      )
                               ],
                      'layout': go.Layout(title='Ro',
                                          autosize=True
                                          )
                  }
                  )
    ])
])

Here is what's happening with option 1 from accepted answer (which is the one I need to go with). 这是已接受答案的选项1发生的情况(这是我需要使用的答案)。 I'm getting three different sizes plus legend covering some of the pie chart: 我得到了三种不同的大小,加上说明了一些饼图的图例:

在此处输入图片说明

I'm struggling to understand how to re-size dash graphs using CSS because the whole container increases in size rather than the actual graph and I don't know how to target just the graphs themself to make size bigger. 我正在努力了解如何使用CSS来调整虚线图的大小,因为整个容器的大小而不是实际图的大小都会增加,而且我不知道如何仅针对自己的图来增大大小。 Is there a way around this? 有没有解决的办法?

Plotly's domain is used for subplots. Plotly的domain用于子图。 In your case you are plotting three individual plots one after the other and for each you are setting the domain separately. 在您的情况下,您要一个接一个地绘制三个单独的图,并且每个图都分别设置域。

You have at least two options: 您至少有两个选择:

  1. Use the approach you are using now, ie 3 individual plots, and use CSS to define their position 使用您现在使用的方法,即3个单独的图,并使用CSS定义其位置
  2. Create one plot with three figures and use domain to adjust their position. 创建一个包含三个图形的绘图,并使用domain调整其位置。

Option 1 选项1

import dash
import flask
import dash_html_components as html
import plotly.graph_objs as go
import dash_core_components as dcc

server = flask.Flask('app')
app = dash.Dash('app', server=server,
                external_stylesheets=['https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'])

labels = [['monkeys', 'elephants'],
          ['birds', 'dinosaurs'],
          ['unicorns', 'giraffes']]

values = [[50, 40],
          [100, 10],
          [100, 20]]

data = []

for label, value in zip(labels, values):
    data.append(html.Div([dcc.Graph(figure={'data': [go.Pie(labels=label,
                                                            values=value,
                                                            hoverinfo='label+value+percent', textinfo='value'
                                                            )]})
                          ], className='col-sm-4'))

app.layout = html.Div(data, className='row')

app.run_server()

在此处输入图片说明

Option 2 选项2

import dash
import flask
import dash_html_components as html
import plotly.graph_objs as go
import dash_core_components as dcc

server = flask.Flask('app')
app = dash.Dash('app', server=server)

labels = [['monkeys', 'elephants'],
          ['birds', 'dinosaurs'],
          ['unicorns', 'giraffes']]

values = [[50, 40],
          [100, 10],
          [100, 20]]

data = []
x1 = 0
x2 = 0.25
for label, value in zip(labels, values):
    data.append(go.Pie(labels=label,
                       values=value,
                       hoverinfo='label+value+percent', textinfo='value',
                       domain={'x': [x1, x2], 'y': [0, 1]}
                                      )
                )
    x1 = x1 + 0.30
    x2 = x1 + 0.25



app.layout = html.Div([
    html.Div([dcc.Graph(figure={'data': data})])
])


app.run_server()

在此处输入图片说明

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

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