import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
inline
command tells jupyter to give output below the cells instead of a popup.Line Chart :
plt.plot([list or series of values]);
- Plots a simple 2d line chart.
plt.plot(x,y);
- Pass 2 series or lists to plot x and y axis. Invoke multiple plt.plot()
statements in a single cell of code to add multiple lines to the same graph.
plt.plot()
-fmt
argument (shortcut) →plt.xlabel('text')
- Gives label to the axis. (x or y).
plt.title('text')
- Title of the graph.
plt.legend([list])
- legend creation. (index kinda)
plt.figure(figsize =(n,n))
- Changes the size and aspect ratio of the whole graph.
Using Seaborn for styling :
Seaborn contains a set of predefined styles. We use sns.set_style("stylename")
eg. whitegrid, darkgrid etc.
matplotlib.rcParams()
This can be edited for direct styling instead of using seaborn. egsns.load_dataset('dataset')
- returns a pandas dataframe from a predefined dataset.Scatter Plot :
Scatter plot is used to visualize the relationship between two variables as points on a two dimensional grid.
When none of the x or y values are ordered (unlike years which is ordered), use scatter plot instead of line chart.
sns.scatterplot(x,y, hue = h, s, data = dataframe)
-
groupby()
on.Histogram :
plt.hist(series of[list of series], bins = n or [val],stacked = True/ False)
- Plots a histogram for given series.
stacked
will stack plots over one another when list of series' is provided.Bar Chart -
plt.bar(x, y);
- Use this to plot a bar chart.plt.bar()
and plt.plot()
can be combined in one cell to see both bar and line chart in one graph.plt.bar(x, y, bottom = 'series')
- can be stacked with another plt.bar() and here two datasets can be stacked for common x axis values. We have to provide 'bottom =' value in the second plt.bar() command to specify which bar is bottom.sns.barplot(x, y, data ='dataframe', hue =z)
- Seaborn provides helper functions for plotting bar charts of averages. It automatically groups by the x axis value and then computes the averages from the dataframe. More:Heatmap :
Heatmap is used to represent 2 dimensional data or a matrix.
.pivot(s1, s2, s3)
- A method for dataframes, this will turn a series in 2 dimensions with s1 series being the rows, s2 series being the columns and s3 series being the values of s3 corresponding to s1 and s2. ‣
sns.heatmap(dataframe, annot = True/False, cmap = 'Blues')
- Turns a 2D dataframe into a heatmap.
annot = True
will include the values in the heatmap.cmap
- contains prebuilt color maps for visualizing a heatmap.Images :
matplotlib can also be used to display images.
Before image is displayed, it has to be imported by the memory. We use from PIL import Image
. PIL = Python Image Library.
image.open(filename.jpg)
- loads the image.
plt.imshow(filename.jpg)
- Opens the image using matplotlib
An image loaded using PIL is simply a 3-dimensional numpy array containing pixel intensities for the red, green & blue (RGB) channels of the image. We can convert the image into an array using np.array
. img_array = np.array(img)
Plotting multiple charts in a grid :
plt.tight_layout(pad=2);
- To set padding
fig, axes = plt.subplots(row, columns)
: gives multiple charts in given shape in a single grid.
Returns two variables fig
and axes
Axes is a numpy array of same shape with axes subplot object. Each axes object can be used to plot a subplot. like use axes instead of plt
eg.
fig, axes = plt.subplots(2, 3, figsize=(16, 8))
# Use the axes for plotting
axes[0,0].plot(years, apples, 's-b')
axes[0,0].plot(years, oranges, 'o--r')
axes[0,0].set_xlabel('Year')
axes[0,0].set_ylabel('Yield (tons per hectare)')
axes[0,0].legend(['Apples', 'Oranges']);
axes[0,0].set_title('Crop Yields in Kanto')
sns
we first provide axes and below that we provide for eg.sns.scatterplot(s1, s2, hue, ax=axes[row,column])
ax argument is used to define the axis.
eg.
# Pass the axes into seaborn
axes[0,1].set_title('Sepal Length vs. Sepal Width')
sns.scatterplot(flowers_df.sepal_length,
flowers_df.sepal_width,
hue=flowers_df.species,
s=100,
ax=axes[0,1]);
Pair plots with Seaborn :
Seaborn also provides a helper function sns.pairplot(dataframe, hue = '')
to automatically plot different types of charts for pairs of features within a dataframe.