Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build a plot from JSON with plotly.js

I have built a plot using plotly.py and convert it to JSON file:

plotly.io.write_json(fig, 'name.json')

After creating the JSON file I need to build the same plot using plotly.js as well. How can build it from generated JSON file? File consists of two main parts. first part is data (parameter of about 100+ lines from plot) and second part is layout of plot. Is there any standard function in plotly.js to build a plot from JSON generated by plotly rules?

like image 702
ANDAHAY Avatar asked Jul 09 '26 02:07

ANDAHAY


1 Answers

This is possible in javascript by first importing your file and then grabing the data, layout and config keys.

import testData from './name.json';

Plotly.newPlot('myDiv', testData.data, testData.layout, testData.config);
<head>
    <!-- Load plotly.js into the DOM -->
    <script src='https://cdn.plot.ly/plotly-2.11.1.min.js'></script>
</head>

<body>
    <div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>
</body>
like image 121
Stigson Avatar answered Jul 10 '26 16:07

Stigson



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!