The Difference Between plot and newPlot Methods in Plotly.js

In Plotly.js, the basic methods to draw a plot are Plotly.plot and Plotly.newPlot and the 2 required parameter for both go them are the id of the plot container and data. If you don't give it data, Plotly.js will only draw an empty plot, so data is sort of required.

Minialistic Example

Plotly.newPlot(
	'plotContainer_1',
	 [{x:[1,2,3],y:[1,2,3]}],
);

The Difference Between Plot and NewPlot

The difference bwterrn Plotly.plot and Plotly.newPlot is that Plotly.plot will plot on top of the same plot without reploting the whole plot.

Example of Using Plotly.plot()

Click this button multiple times to see the effect.
function plot_1(){
	var xArray = [];
	var yArray = [];
	for(let i = 0; i < 10; i++){
		xArray.push(i);
		yArray.push(Math.random());	
	};

	Plotly.plot(
	'plotContainer_2',
	 [{x:xArray,y:yArray}],
	);
};

Example of Using Plotly.newPlot()

function plot_2(){
	var xArray = [];
	var yArray = [];
	for(let i = 0; i < 10; i++){
		xArray.push(i);
		yArray.push(Math.random());	
	};

	Plotly.newPlot(
	'plotContainer_3',
	 [{x:xArray,y:yArray}],
	);
};

When Do I Use Plotly.plot()

I use Plotly.plot() when I build a Monte Carlo Simulator where every result should be shown to users.

Example: Porfoio Value Simulator

Mean Annual Return Rate: %
Annual Return Rate Standard Deviation: %
Duration: year(s)

When Do I Use Plotly.newPlot()

I use Plotly.newplot() when I need to redraw the plot everytime I interavt with some app on my website. This means I want a total new plot after every update of data.

Comments

Popular posts from this blog

How to Make A Reusable Image Slideshow HTML Component With Vanilla JavaScript

HTML Tags and Inline CSS that Work In Plotly.js Title

How to Type Spaces In HTML Input And Display In the Browser