Udemy Visualization Flashcards
When using jupyter notebook, how can you see plots that you create with matplotlib?
%matplotlib inline
Object-oriented way to make an x, y line plot using matplotlib
fig = plt.figure() #essentially creates an empty canvas
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axes.plot(x, y)-===============================
Object-oriented way to create subplots using matplotlib
fig, axes = plt.subplots(nrows=1, ncols=2)
axes[0].plot(x,y)
axes[0].set_title(‘First Plot’)
axes[1].plot(y, x)
axes[1].set_title(‘Second Plot’)
What usually takes cares of overlapping plots?
plt.tight_layout()
How can you control the figure size?
fig = plt.figure(figsize=(width, height))
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(width, height))
How do you save a figure?
fig.savefig(‘my_picture.png’, dpi=200)
Object-oriented way to add y and x label and title
ax. set_ylabel(‘Y’)
ax. set_xlabel(‘X’)
ax. set_title(‘Title’)
How to add a legend
Add label to each plot and follow with ax.legend()
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax. plot(x, x**2, label =‘X Squared’)
ax. plot(x, x**3, label =‘X Cubed’)
ax.legend()
How do you change position of the legend?
number 0-10 for different positions. Use 0 to make it find the “best” location
ax.legend(loc = number)
How do you add color to a line plot?
color can be color name, e.g. ‘green’, or RGB Hex Code starting with
ax.plot(x, y, color = ‘color’)
How do you modify the line width and transparency?
alpha defines transparency
ax.plot(x, y, linewidth = num, alpha = num)
or
ax.plot(x, y, lw = num, alpha = num)
How do you modify the linestyle?
style looks like ‘–’ or ‘:’ . More styles available
ax.plot(x, y, linestyle = ‘style’)
or
ax.plot(x, y, ls = ‘style’)
How do you add markers to each point on a line plot?
How do you modify the marker size?
Several marker styles available
ax.plot(x, y, marker = ‘marker’, markersize = num)
How do you set the max num in the x and y axes?
ax. set_xlim([lower, upper])
ax. set_ylim([lower, upper])
seaborn is built on top of
matplotlib