Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change plot background color

Looking for advice of how to change the background color of the plotly plot to transparent?

I have looked around the docs, but could not find any reference.

pie_chart = {
  'data': [{
    'labels': ['V0', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9'],
    'values': [55, 22, 31, 32, 33, 45, 44, 42, 12, 67],
    'type': 'pie',
    'sort': false,
  }],

  'layout': {
    backgroundColor: "rgba(255,255,255,0.5)",
    width: 320,
  }

}

Plotly.newPlot('plot', pie_chart.data, pie_chart.layout);
#plot {
  background-color: lightblue;
}
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="plot"> </div>
like image 940
Idan Avatar asked Aug 31 '26 10:08

Idan


2 Answers

Please use paper_bgcolor property of layout to make it transparent.

paper_bgcolor (color):
default: "#fff"
Sets the color of paper where the graph is drawn.

From the official documentation, available here

pie_chart = {
  'data': [{
    'labels': ['V0', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9'],
    'values': [55, 22, 31, 32, 33, 45, 44, 42, 12, 67],
    'type': 'pie',
    'sort': false,
  }],

  'layout': {
    paper_bgcolor: "rgba(0,0,0,0)",
    width: 320,
  }

}

Plotly.newPlot('plot', pie_chart.data, pie_chart.layout);
#plot {
  background-color: lightblue;
}
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="plot"> </div>
like image 200
Naren Murali Avatar answered Sep 03 '26 00:09

Naren Murali


plot_bgcolor sets the color of plotting area in-between x and y axes.

paper_bgcolor sets the color of paper where the graph is drawn (= outside of axes but inside of parent div).

So for a transparent background you need to use both in layout section like this:

layout = {
   plot_bgcolor: "rgba(0,0,0,0)",
   paper_bgcolor: "rgba(0,0,0,0)"
}

Official reference: https://plot.ly/javascript/reference/#layout-paper_bgcolor

like image 39
Donat Szecsko Avatar answered Sep 02 '26 22:09

Donat Szecsko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!