-
Basics Review 1
-
Lecture1.1
-
-
Subplots 2
-
Lecture2.1
-
Lecture2.2
-
-
3D Plots 2
-
Lecture3.1
-
Lecture3.2
-
-
Animation 1
-
Lecture4.1
-
Line Plot Options
Line Plot Options¶
This part of the course is to simply make sure you understand the very basics of line plotting with matplotlib because we will jump into to some really nice advanced techniques that will really bring your visualization game to the next level afterwards. This course will cover three advanced topics, subplots, 3d plotting and animations. These three topics are some of the more useful features in matplotlib that will save you a lot of time and effort.
To review, if we make some random data, we can plot it with plt.plot(X,Y) giving X and Y points for the graph.
import matplotlib.pyplot as plt
#Create values
X = list(range(21))
Y = [val **2 for val in X]
#Plot the points
plt.plot(X, Y)
plt.show()
Calling plt.xlabel() and plt.ylabel() assigns axis labels.
#Add axis labels
plt.plot(X, Y)
plt.xlabel("Time")
plt.ylabel("Sales")
plt.show()
A title can be added with plt.title().
plt.plot(X, Y)
plt.xlabel("Time")
plt.ylabel("Sales")
#Add title
plt.title("Sales Growth")
plt.show()
Given the parameter color in the plt.plot function we change line color.
#Change the color
plt.plot(X, Y, color='red')
plt.xlabel("Time")
plt.ylabel("Sales")
plt.title("Sales Growth")
plt.show()
The parameter marker in this same function can set a market for the points. The matplotlib documentation details all possible markers, but simply passing “D” will get us a diamond marker.
#Change the marker to a diamond
plt.plot(X, Y, color='red', marker='D')
plt.xlabel("Time")
plt.ylabel("Sales")
plt.title("Sales Growth")
plt.show()
The parameters markerfacecolor and markeredgecolor control the colors on the markers.
#Change the color of the marker
plt.plot(X, Y, color='red', marker='D',
markerfacecolor='green', markeredgecolor='blue')
plt.xlabel("Time")
plt.ylabel("Sales")
plt.title("Sales Growth")
plt.show()
For sizing, we have markersize and linewidth to modify which control the size of the market and the width of the line.
#Change line width and marker size
plt.plot(X, Y, color='red', marker='D',
markerfacecolor='green', markeredgecolor='blue',
markersize=8, linewidth=4)
plt.xlabel("Time")
plt.ylabel("Sales")
plt.title("Sales Growth")
plt.show()
The linestyle allows for changing the line from a regular line to something like a dotted line.
#Change line style
plt.plot(X, Y, color='red', marker='D',
markerfacecolor='green', markeredgecolor='blue',
markersize=8, linewidth=4, linestyle="dotted")
plt.xlabel("Time")
plt.ylabel("Sales")
plt.title("Sales Growth")
plt.show()
Finally, plt.axhline and plt.axvline draw horizontal and vertical lines respectively which is good for when you want some sort of baseline.
plt.plot(X, Y, color='red', marker='D',
markerfacecolor='green', markeredgecolor='blue',
markersize=8, linewidth=4, linestyle="dotted")
#Plot a vertical line at x=10
plt.axvline(10, color='grey')
#Plot a horizontal line at y=200
plt.axhline(200, color='black')
plt.xlabel("Time")
plt.ylabel("Sales")
plt.title("Sales Growth")
plt.show()