Skip to main content
SAGE
Search form
  • 00:00

    [MUSIC PLAYING]

  • 00:04

    GARY WHITE: This time we're goingto look at working with 3D figures in my Matplotlibso I would advise to use caution when using 3D figuresand to only use them if necessary.So avoid using 3D bar charts where there isn't reallya need for three dimension.That sort of takes away the emphasis of the data.

  • 00:25

    GARY WHITE [continued]: But sometimes, when we're workingwith data that's in three dimensionsand it has an x, y, and z axis, weneed to use three dimensions.So Matplotlib is actually quite good at doing this.So we're going to look at that in this section,so we're going to look at some pointand lines, the counter plots, and some surface spots.

  • 00:45

    GARY WHITE [continued]: So as usual, I've imported NumPy and imported Matplotlib as plt.And we're going to set up our access.And we're going to use plt.axis.And we're going to set the projection equal to 3d.

  • 01:10

    GARY WHITE [continued]: And that needs to be in quotations, so projection="3d"And now we can start generating our datathat we're going to use for our 3D plot,so we're going to have a z line.And that's going to be equal to np.linspace,

  • 01:34

    GARY WHITE [continued]: and it's going to be from 0 to 15.And we're going to have 1,000 points.We're then going to create our x line,and that's going to be = np.sin (zline).And we're going to duplicate that and say that's our y line.

  • 01:56

    GARY WHITE [continued]: So yline= np.sin(zline) So x line,and we're going to have the y line actually equalto np.cos(zline)So we can create a 3D scatterplot of the data

  • 02:17

    GARY WHITE [continued]: by using ax.plot3DAnd to create this, we need our x line, our y line, and thenour z line.And we can also specify all of the standard formatting optionsthat we've used before.So we want to set the color equal to gray.

  • 02:38

    GARY WHITE [continued]: And so, if we plot just this to showwhat we've created so far using plt.show we can run that.And you see here, we have our curved line,which is going in three dimensional space.And it's a 3D plot of the x line, y line and z line,

  • 02:59

    GARY WHITE [continued]: and it's this gray-colored line here.We're going to add a bit more detail to the plot,so we're going to add a number of points.So we're going to say z data = 15 x np.random

  • 03:20

    GARY WHITE [continued]: And then we're going to use the random functionto generate 100 random values.Then we're going to say that our x data =to np.sin the z data + 0.1.

  • 03:48

    GARY WHITE [continued]: x the np.random.randn(100) So we're generating another 100random values, and we can duplicate that and use itfor our y data is equal to the cosine of the z data.

  • 04:15

    GARY WHITE [continued]: And we're also multiplying it by 100 random values,and then we're going to generate a scatterplot using ax.scatterAnd, in this case, it's a 3D scatterplot, so scatter 3D.Then we specify our x data, our y data, and our z data.

  • 04:43

    GARY WHITE [continued]: And we're going to set the color equal to the z data,and we're going to use the color map of greens.So if we run that again, you can see now we have these.So the original plot, the 3D plot, which went like this,

  • 05:08

    GARY WHITE [continued]: and now we have the x data, y data,and z data, which has some random values around it.So you can see that it follows the curve with some errorand follows it all the way up through the three dimensions.So this is how we can create a plot in 3D.On this green color scale, you cansee how it changes as the z data increases,

  • 05:30

    GARY WHITE [continued]: so as it gets further and further,the color map goes darker and darker.So it's very bright here, and then gets darkeras the color goes up.

Video Info

Series Name: Data Visualization With Matplotlib 3.x and Python

Episode: 43

Publisher: Gary White

Publication Year: 2020

Video Type:Tutorial

Methods: Data visualization, Python, Coding

Keywords: coding; data visualisation; graphical presentation of data; interactivity; Scatterplot

Segment Info

Segment Num.: 1

Persons Discussed:

Events Discussed:

Keywords:

Abstract

Gary White explains how to work with 3D figures in Matplotlib.

Looks like you do not have access to this content.

3D Points and Lines

Gary White explains how to work with 3D figures in Matplotlib.

Copy and paste the following HTML into your website