Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly domain variable explained (Multiple graphs)

Tags:

python

plotly

I am looking at the 'Pie Chart Subplots' section (last one on page) of the following tutorial https://plot.ly/python/pie-charts/.

I cant figure out how to understand the domain variable when it comes to plotting multiple graphs at once. For instance:

        {
        'labels': ['1st', '2nd', '3rd', '4th', '5th'],
        'values': [38, 27, 18, 10, 7],
        'type': 'pie',
        'name': 'Starry Night',
        'marker': {'colors': ['rgb(56, 75, 126)',
                              'rgb(18, 36, 37)',
                              'rgb(34, 53, 101)',
                              'rgb(36, 55, 57)',
                              'rgb(6, 4, 4)']},
        'domain': {'x': [0, .48],
                   'y': [0, .49]},
        'hoverinfo':'label+percent+name',
        'textinfo':'none'
    },

will plot a pie chart in the bottom left coordinates.

Can someone explain how the domain variable (and its x and y components) works in practice in plotly?

like image 712
KBoehme Avatar asked Mar 06 '17 23:03

KBoehme


People also ask

How do you use subplots in plotly?

Simple SubplotFigures with subplots are created using the make_subplots function from the plotly. subplots module. Here is an example of creating a figure that includes two scatter traces which are side-by-side since there are 2 columns and 1 row in the subplot layout.

What is domain in plotly?

In short, domain just specifies which space of the total plot area is occupied by the subplot.

What does Add_trace do in plotly?

Adding TracesNew traces can be added to a graph object figure using the add_trace() method. This method accepts a graph object trace (an instance of go. Scatter , go. Bar , etc.)

Are plotly plots interactive?

Plotly produces interactive graphs, can be embedded on websites, and provides a wide variety of complex plotting options. The graphs and plots are robust and a wide variety of people can use them. The visuals are of high quality and easy to read and interpret.


1 Answers

Let's just use Javascript because it can be executed here. The domain attribute is identical in Python and Javascript.

From the documentation:

domain

x (array)

default: [0, 1]

Sets the horizontal domain of this pie trace (in plot fraction). Each object has one or more of the keys listed below.

The first member of the array is the start position and the 2nd member is the end position.

For x 0 means all the way to the left and 1 is all the way to right.

For y 0 is top and 1 is bottom.

e.g. the upper left quadrant would be x = [0, 0.5] and y = [0, 0.5]


If you have multiple plots, e.g. in the example below there are two divs with 4 plots each, the domain of each plot specifies which space is occupied by it. In the first example each plot gets a quarter of the available space (i.e. x and y are set to 0 to 0.5 resp. 0.5 to 1).

In the second example the domain of the last plot overlaps with the other domains (0.25 to 0.75).

In the third example the domain of the last plot is bigger and overlaps with the rest (0 to 0.75).

In short, domain just specifies which space of the total plot area is occupied by the subplot.

var allValues = [
  [50, 30, 20],
  [45, 30, 25],
  [55, 25, 20],
  [50, 15, 35]
];

var data = [{
  values: allValues[0],
  type: 'pie',
  domain: {
    x: [0, .5],
    y: [0, .5]
  },
}, {
  values: allValues[1],
  type: 'pie',
  domain: {
    x: [0.5, 1],
    y: [0, .5]
  }
}, {
  values: allValues[2],
  type: 'pie',
  domain: {
    x: [0, .5],
    y: [.5, 1]
  }
}, {
  values: allValues[3],
  type: 'pie',
  domain: {
    x: [0.5, 1],
    y: [0.5, 1]
  },
}];



Plotly.newPlot('myDiv', data);
data[3].domain.x = [0.25, 0.75];
data[3].domain.y = [0.25, 0.75];

Plotly.newPlot('myDiv2', data);

data[3].domain.x = [0.0, 0.75];
data[3].domain.y = [0.0, 0.75];
Plotly.newPlot('myDiv3', data);
<div id='myDiv'></div>
<div id='myDiv2'></div>
<div id='myDiv3'></div>

<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
like image 158
Maximilian Peters Avatar answered Sep 22 '22 12:09

Maximilian Peters