import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
  1. Line Chart :

    1. plt.plot([list or series of values]); - Plots a simple 2d line chart.

    2. 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.

      • ←(click to show content) Supported arguments for plt.plot() -
      • fmt argument (shortcut) →
    3. plt.xlabel('text') - Gives label to the axis. (x or y).

    4. plt.title('text') - Title of the graph.

    5. plt.legend([list]) - legend creation. (index kinda)

    6. plt.figure(figsize =(n,n)) - Changes the size and aspect ratio of the whole graph.

    7. Using Seaborn for styling :

    8. Seaborn contains a set of predefined styles. We use sns.set_style("stylename") eg. whitegrid, darkgrid etc.

      • To know more :
    1. Scatter Plot :

    2. Scatter plot is used to visualize the relationship between two variables as points on a two dimensional grid.

    3. When none of the x or y values are ordered (unlike years which is ordered), use scatter plot instead of line chart.

    4. sns.scatterplot(x,y, hue = h, s, data = dataframe) -

      1. creates a scatter plot from two series' of values x and y.
      2. h is hue. Hue takes another series of same length as x and y, and color codes points(x,y) accordingly. Hint - Hue can be any series that we use groupby() on.
      3. s - size of points , float.
      4. Seaborn has great support for padas. So instead of loading data prior, we can pass in a dataframe object as an argument for the graph from which x, y and h are obtained.
      • eg
    5. Histogram :

      1. A histogram represents the distribution of data by forming bins along the range of the data and then drawing bars to show the number of observations that fall in each bin.
      2. the values are on x axis and number of values that lie in a certain interval are on y axis.
      3. Uses one series / column.
      4. plt.hist(series of[list of series], bins = n or [val],stacked = True/ False) - Plots a histogram for given series.
        1. Bins is used to control intervals. n will create n intervals.
        2. A list of values can also be passed to create bins of set intervals.
        • eg.
        • stacked will stack plots over one another when list of series' is provided.
    6. Bar Chart -

      1. Pretty similar to line chart as it shows sequence of values, but instead of connecting points to form a line, we plot bars for each values.
      2. Line charts are good for studying trends but bar charts are useful for comparing values.
      3. plt.bar(x, y); - Use this to plot a bar chart.
      4. plt.bar() and plt.plot() can be combined in one cell to see both bar and line chart in one graph.
      5. 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:
      1. Heatmap :

      2. Heatmap is used to represent 2 dimensional data or a matrix.

      3. .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. ‣

      4. sns.heatmap(dataframe, annot = True/False, cmap = 'Blues') - Turns a 2D dataframe into a heatmap.

        1. annot = True will include the values in the heatmap.
        2. cmap - contains prebuilt color maps for visualizing a heatmap.
      5. Images :

      6. matplotlib can also be used to display images.

      7. Before image is displayed, it has to be imported by the memory. We use from PIL import Image . PIL = Python Image Library.

      8. image.open(filename.jpg) - loads the image.

      9. plt.imshow(filename.jpg) - Opens the image using matplotlib

      10. 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)

      11. Plotting multiple charts in a grid :

      12. plt.tight_layout(pad=2); - To set padding

      13. fig, axes = plt.subplots(row, columns) : gives multiple charts in given shape in a single grid.

      14. Returns two variables fig and axes

      15. 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

      16. 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')
      
        1. For 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]);
        
    7. Pair plots with Seaborn :

    8. Seaborn also provides a helper function sns.pairplot(dataframe, hue = '') to automatically plot different types of charts for pairs of features within a dataframe.