 Here we are in another tutorial and this time around we're going to look at subplots. How can we add a few more plots to the same figure? Let's run our first cell there just importing our cascading style sheet. There we go. We're going to import iPlot and init notebook mode from plotly.offline and we're going to initialize this in notebook mode. I'm also going to import the high-level chart objects as go and a new one. We're also going to import tools just from the plotly library. Also importing numerical Python and then we're going to see the serial number generator just with a value of 1234. First of all, two plots on the same row. So I'm going to create two traces. They're both going to be scatter plots. Now we've created this data, data that we've seen before, just a random integer chosen from a low of 20, a high of 65 and 100 of those. We're going to call that female age, then male age, female salary, male salary and now we have debt and we have female tax equals male tax. Just a bunch of random data that we've created. We've looked at it before. So trace zero is a scatter plot on the y-axis. We're going to have the female salary on the x-axis I should say and on the y-axis female age. We have markers as the mode, so not lines or lines and markers, just markers. We're going to call this female and the marker size. It's going to have a certain color and a certain size. We're going to have trace one also scatter exactly the same thing. Here comes the new part. I'm going to create a figure, a figure object here. I'm just going to call it fig and that's going to be tools.make underscore subplots and that's how you do those. And I want one row in two columns. If you think about it, if there's a single row in two columns, these plots are going to be next to each other and I'm going to give each subplot a title. So I'm also going to use the argument subplot underscore titles and I'm going to use this list of female and male. Now we need to append the traces to this figure. So we're going to say fig dot append underscore trace, trace zero goes in position row one column one and trace one goes in row one column two. So very easy to figure out how this is going to work. Fig dot layout dot update can also update a few more things in this instance just a title. Let's run this and see what it looks like. And there we go. We see the two plots side by side, the subplot with the name drawn from the name and the trace, male on this side, the colors we've drawn in and we see the plots side by side. So our first subplot. Now if we look at it, the y-axis here are exactly the same 20 and 20, 30 and 30, we might as well just have one on the left hand side. So let's look at how to share that y-axis. Again the two traces, nothing has changed. The two, the subplots I've created, one row two columns, female and male. I've now added a new argument called shared underscore y-axis, es. And we're going to set that to true. Everything else is exactly the same. And if we run this, we see indeed that the axis on this right hand side, the male side, has disappeared. We are now sharing the same y-axis. Okay, what about two rows in one column? So all we're going to do is just to shift this around with make underscore subplots here, we're going to have two rows in one column. And we're going to say a shared x-axis. And you've got to think about this, these two traces that you are creating or the number of traces that you're creating, they've got to have the same domain and range here. Otherwise this might not make a lot of sense. So you've got to think about that. And if we run that, we see we have two rows in one column and also we're sharing this single x-axis. And no matter where we are, we can still get all the data because this is plotly, no matter where we hover, we're still going to get our data. So what about constraining the proportions? Now this one gets a bit more difficult. And we need to pay a bit of attention here. We're still going to create our trace, that's a scatter. But our trace, the second one we're creating here, trace one, I'm going to put two new arguments. And that is x-axis equals x2 has a string and y-axis equals y2, telling plotly that this goes on a different axis altogether. Now we don't need to do that if we're not doing anything fancy as we've seen before. But here we want to do something. So I'm going to use a different way of creating the data. That's a Python list, trace zero and trace one. And I'm going to have a separate layout as we've had before in all the other tutorials that we've had. So let's go dot layout. I'm still going to have a title. I'm going to have an x-axis. And I'm going to pass a dictionary to that. And that dictionary is a domain. And think about this x-axis going from zero to one. So zero to 100% of the width of what you have available. And in the domain we're saying use the leftmost 70% of the area of our figure for our first trace. So the x-axis goes from zero to 0.7. x-axis two, remember which comes in this one here we said x-axis two equals x2. So x-axis two goes from 80% to 100%. So that 20% on the right hand side. And we just have to say that y-axis two, so the second plot is anchored to x2. So it will know that it has to anchor this second y-axis to this part that we've specified up here. So go and mess it about and put it in the first one. And then figure equals go dot figure. So we've done it this way around. Remember you could just say I plot and use the standard dictionary format that we've seen before. So let's run that and now we can see we're constraining this right hand side to the last 80% and 70% on this side. And this y-axis was told to fit with this little one on the right hand side. Okay let's customize the axis a little bit more. Here's a long one for you. We're just creating these four traces, they're all histograms. We're going to make a subplot and we're going to say we want two rows and two columns. We give a name to each of those, you don't have to, but we're going to call it figure A, B, C and D. And I append each of those to the position that I want, row one column one, row one column two, row two column one, row two column two. So it's always row first and then column and it's got to line up with what you specified up here. So fig.layout, x-axis one, updated title, x-axis two updated title, x-axis three and four and then y-axis one, two, three, four. These have all got to line up with the number of plots you've created there and I can give each of the axis its own separate title. We're just going to update to the main title and one new thing here, I'm just going to say show legend equals false because I'm building in all the detail into each of these little subplots. Let's run that and there we go. We get our four histograms that we created here, figure A, B, C, D. They've all got the axis, they're all nicely labeled, no problem whatsoever. So you can see female agent frequency, male agent frequency, female salary frequency, male salary frequency. So I can individually name each of these axes so we know what it's all about. Let's just look lastly at the odd pairing. So I'm going to introduce a new argument here to the tools.make subplots. I want two rows and two columns but you can see I've only listed three things here. That's because I want the bottom two to be combined and I want it to make up one single long plot. So two at the top, the first row will have two columns, the second row just a single column so it's got to go all the way across. So I've got to introduce the specs. Now look at it, we see it's a list and it's a list in two parts. The first part is going to be for this first row, just showing the two there and the second one we're going to say the column span spans both of the columns and then none because it is just the full thing that we want because we only want these three figures. Otherwise everything is exactly the same as we've seen before and there we go. See what we did? It spans the whole. The third one here spans this whole row on both sides. So look and play around with this specs argument but you can clearly see how that was built up for the two rows and we want those two in the first row but we want a single one in the second row there. And that is subplots. Have some fun enjoying your subplots. It really helps to be able to do subplots and especially if you're creating reports or manuscripts for publication to have more than one element in this same plot. Remember to subscribe and hit that like button. If you want to hit the notification button though that's important so that you can get notified if new material is released. Remember that you can also find all of these files on github. A link is in the description below. Speak to you next time.