Python for Visualization Flashcards
Pandas function that converts column to datetime type and return a series object
dataframe[‘col’] = pandas.to_datetime(df[‘col’])
Library used to create charts
matplotlib
matplotlib module that provides a high-level interface that allows us to quickly create common data plots and perform common tweaks to them
pyplot
Code to import pyplot module
import matplotlib.pyplot as plt
pyplot functions to (1) create chart and (2) display
plt. plot(x_values, y_values, c=’color’, label=’label’)
plt. show()
For x and y values, matplotlib will accept any iterable object, like NumPy arrays and pandas.Series instances
pyplot function to customize behavior of x-axis ticks
plt.xticks()
pyplot function to modify x-axis labels
plt.xlabel()
accepts a string value, which gets set as the x-axis label
pyplot function to modify y-axis labels
plt.ylabel()
accepts a string value, which is set as the y-axis label
pyplot function to modify chart title
plt.title()
accepts a string value, which is set as the plot title
pyplot function to manually create a figure
A figure acts as a container for all plots and has methods for customizing the appearance and behavior for the plots within that container.
fig = plt.figure()
Method to add a subplot to an existing figure, which will return a new axes object that needs to be assigned to a variable
axes_obj = fig.add_subplot(nrows, ncols, plot_number)
Creating a figure with 2 plots, one above the other
ax1 = fig.add_subplot(2,1,1) ax2 = fig.add_subplot(2,1,2)
Parameter to adjust dimensions of the plotting area
figsize (use when calling plt.figure())
fig = plt.figure(figsize=(width, height))
Width & height are in inches
Method to add a title to axes object
axes. set_title
ax1. set_title(‘Chart Name’)
Built in type that represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops
range(start, stop[, step])
Ex: for i in range(5)
Produces a sequence of integers starting at zero, and going up to (but not including) the argument’s value
pyplot function to create a legend
plt.legend(loc=’upper left’)
Method to place a legend on the axes
axes.legend
pyplot function to create a bar chart
plt.bar() or axes.bar(left=[position], height=[height], width=[width])
axes.bar allows easier customization
Ex: ax.bar(left=bar_positions, height=bar_heights, width=.5)
Shortcut to generate a single subplot and return both the figure and axes object
fig, ax = plt.subplots()
numpy function that returns evenly spaces values
np.arange([number of values to generate])
Ex:
from numpy import arange
bar_positions = arange(5) + .75
Method to change positions of x ticks
axes.set_xticks()
Ex:
tick_positions = range(1,6)
ax.set_xticks(tick_positions)
Method to specify tick labels
axes.set_xticklabels()
Ex:
num_cols = [list]
ax.set_xticklabels(num_cols)
pyplot function to create a bar chart
axes.barh(bottom=[y coord], width=[length of bars])
Methods to specify axes labels
axes. set_xlabel
axes. set_ylabel
___ to generate scatter plot
axes.scatter(x=’x_values’, y=’y_values’)
Sets data limits for axes
axes. setxlim(min, max)
axes. setylim(min, max)
Method to generate a histogram
axes. hist()
ax. hist(df[‘col’], bins=10, range=(0, 5))
Method to generate a boxplot
axes.boxplot()
ax. boxplot(norm_reviews[‘RT_user_norm’])
ax. set_xticklabels([“Rotten Tomatoes”])