Mountains are fun! Here, check out this mountain on Google Maps:
The reason I'm showing you this is because you're going to make your own mountain with Python! 🐍🚀
Once your fork loads, you should notice a blank file called
main.py and a file called
mountain.csv, which contains some data. If you see this, you're ready to move on to the next step!
We're going to use 3 libraries for this workshop:
Start by importing these libraries at the top of the
import pandas as pd import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D
pandasallows importing and working with data from datasets. We're going to use it to manage our CSV file.
numpyis a library that allows for easy scientific computing. We're going to use it to manage arrays in this workshop.
matplotlibis a library for data visualization—histograms, scatter plots and bar graphs, etc. We're going to use it to make our mountain.
mpl_toolkitsare collections of functions that extend the
matplotlibapplication. This will enable us to plot in a 3D plane, rather than 2D.
Once you've imported these libraries, add a blank line, then add:
DataFrame = pd.read_csv('mountain.csv')
Here, we're creating a variable called
DataFrame, which will use
pandas to read the
Under that line, add:
DataFrame = DataFrame.unstack()
unstack() function unstacks the row to columns. Here's a diagram that shows how it works:
Under that, add:
DataFrame = DataFrame.reset_index()
When you concatenate, sort, join or do some rearrangements with your
DataFrame, the index gets shuffled or out of order. To reset the index of a
DataFrame, we use
reset_index() to resort the indexes.
Uunder that, add:
DataFrame.columns = ['X', 'Y', 'Z']
DataFrame contains three columns without labels, so we need to assign labels to the columns.
DataFrame.columns assigns the first column to
X, the second to
Y, the third to
Z. In our 3D graph, this will correspond to latitude, longitude, and altitude.
Fun fact: we have exactly 552 coordinates.
Under that, add:
DataFrame['X'] = pd.Categorical(DataFrame['X'])
Categorical is a
pandas data type which is used to save memory space and speed up computation. you can convert using syntax
pd.Categorical() with parameter
DataFrame['X'] = DataFrame['X'].cat.codes
cat.codes, we get unique integer values for each value of
X in an array in the position if the actual values, even if the value is none. Then, it returns a unique numeric value.
Suppose your data contains a column named "Birds" with 100 rows, which has only two types of values—parrot and owl—repeated in rows. Even though we only have two types of data, we have 1000 rows of them so the computer will treat every value as unique. To save memory, we specify the similar set of values as a category, so that the computer doesn't allocate memory every time it encounters that value. Instead, it will just assign a reference to the value. If this sounds like gibberish to you, don't worry—all you need to know is that we're doing some fancy computer memory saving things.
Under this line, add:
fig = plt.figure(figsize=(6, 8))
Here, we're using
plt.figure() to create a figure window and assigning it to a variable called
ax = fig.gca(projection='3d')
fig.gca() with the argument
projection=3d returns the three-dimensional axes associated with the figure window. This is stored in a variable called
ax.plot_trisurf(DataFrame['X'], DataFrame['Y'], DataFrame['Z'], cmap=plt.cm.jet, linewidth=0.2)
This creates a three-dimensional plot.
cmapdefines the colormap of the plot. We're using the
jetcolormap. Learn more about the different types of colormaps here.
linewidth=0.2makes the curves smoother.
plt.title("Mount San Bruno") plt.xlabel("x axis") plt.ylabel("y axis")
plt.titleadds a title to the plot
plot.ylabeladd labels to the x and y-axis of the plot.
Next, let's display the plot!
plt.show opens an interactive window that displays your figure.
import pandas as pd import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D DataFrame = pd.read_csv('mountain.csv') DataFrame = DataFrame.unstack() DataFrame = DataFrame.reset_index() DataFrame.columns = ['X', 'Y', 'Z'] DataFrame['X'] = pd.Categorical(DataFrame['X']) DataFrame['X'] = DataFrame['X'].cat.codes fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_trisurf(DataFrame['X'], DataFrame['Y'], DataFrame['Z'], cmap=plt.cm.jet, linewidth=0.2) plt.title("Mount San Bruno") plt.xlabel("x axis") plt.ylabel("y axis") plt.show()
Congrats!!! You've completed the workshop! Pretty simple, right?
Now that you've explored how to make a basic 3D mountain, the possibilities are endless. Real data scientists use Python, along with the tools you used in this workshop, to make complex data visualizations. Here are a few examples I came up with that you can try—but try finding some interesting things you can do in addition to these!
- Example 1, using a CSV from Kaggle to make a 3D Volcano.
- Example 2, using Array, and Cos function to make a Surface plot.
- Example 3, using Loops, List, and Sin function to make the contour plot.