2

I want to plot 2 plots in the same figure using plotly.js. I have gone through the documentation of plotly.js but was unable to find it. Can any body help?

Here is the code snippet:

First Plot:

    var data4 = [ {
        z: z,
        x: x,
        y: y,
        type: 'contour'
    }
];

var layout = {
  title: 'Simple Contour Plot'
}

Plotly.newPlot('visualization2', data4, layout);

Second Plot:

 var trace = {
  x: x11,
  y: x21,
  mode: 'lines+markers'
};

var data3 = [ trace];

 var layout = {
  title:'Line and Scatter Plot',
  height: 400,
  width: 480
 };

Plotly.newPlot('visualization3', data3, layout);

I want to plot these two in one figure. Note: The first one is a contour plot and the second one is a Line plot.

2
  • your code ? your markup ? Commented Jun 20, 2016 at 16:50
  • Sorry for that! I have updated the question with my code. Please look into it Commented Jun 20, 2016 at 18:06

1 Answer 1

4

You have the ability to put a number of traces into your data that you wish to plot and it will plot them.

You also can only have 1 title per graph, however, you can have multiple axis titles.

var firstPlotTrace = {
    z: z,
    x: x,
    y: y,
    type: 'contour'
    name: 'yaxis data'
};

var secondPlotTrace = {
    x: x11,
    y: x21,
    mode: 'lines+markers'
    name: 'yaxis2 data'
};

var plotData = [firstPlotTrace, secondPlotTrace];

var layout = {
    title: 'Double Plot Title',
    height: 400,
    width: 400,
    yaxis: {title: 'Simple Contour Plot Axis', range: [-20, 20]}
    yaxis2: {title: 'Line and Scatter Plot Axis', range: [-20, 20]}
};

Plotly.newPlot('plotDiv', plotData, layout);

HTML -

<div id="plotDiv" style="height: 400px; width: 400px;"></div>
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried this. It is showing both the plots but the contour is shifting the axis by some value. Do you have any idea why this is happening?
@SkandVishwanathPeri what do you mean by that? Is there anyway you can replicate the issue somewhere?
@Adijit here is the code for both the plots together link. The problem is that the lines plot must intersect at (2.-2) as shown in this plot link. In the first plot the line plot is being shifted by some value. Don't know why?
@SkandVishwanathPeri You just need to make the range of both axes the same. If you look, the y-axis 0 on the right is in a different spot than the y-axis 0 on the left. So the y-axes are different for both lines. take a look at the edit. As a side note, if you hover your mouse over the axis you can click and drag to adjust. Since they are 2 different types of lines their axis values are independent of each other

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.