<jupyter_start><jupyter_text>Guided Practice: Explore Python Data Viz_Author: <NAME> (NYC) <NAME> (SF)_---In this practice lab you will use Pandas, Matplotlib, and Seaborn to create simple plots.We'll cover plotting line plots, scatter plots, bar plots, histograms, and how to manipulate the style of your plots with Matplotlib.We will use primarily the Boston Housing data from the UCI repository.<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import scipy.stats as stats

plt.style.use('fivethirtyeight')
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Pandas plotting documentation[Link to Documentation](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html)  Create fake data for examples<jupyter_code>df = pd.DataFrame(np.random.randn(10, 4), 
                  columns=['col1', 'col2', 'col3', 'col4'],
                  index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])<jupyter_output><empty_output><jupyter_text>Load in Boston housing data for exercisesThe data dictionary can be found [here](https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.names)<jupyter_code>housing_csv = './assets/datasets/boston_housing_data.csv'
df = pd.read_csv(housing_csv)
print(df.columns)
df.shape<jupyter_output>Index([u'CRIM', u'ZN', u'INDUS', u'CHAS', u'NOX', u'RM', u'AGE', u'DIS',
       u'RAD', u'TAX', u'PTRATIO', u'B', u'LSTAT', u'MEDV'],
      dtype='object')<jupyter_text>Line plots---  Line plot with a DataFrame<jupyter_code>df.plot.line()<jupyter_output><empty_output><jupyter_text>Line plot with a Series<jupyter_code>df['CRIM'].plot.line();
# series.plot.line()<jupyter_output><empty_output><jupyter_text>Change the size of a plot<jupyter_code>df['CRIM'].plot.line(figsize = [10,1])<jupyter_output><empty_output><jupyter_text>Change the color of a plot<jupyter_code>df['CRIM'].plot.line(color = 'r')<jupyter_output><empty_output><jupyter_text>Change the style of individual lines<jupyter_code>df['CRIM'].plot.line(style = '.')<jupyter_output><empty_output><jupyter_text>Create a line plot of ZN and INDUS in the housing data. For ZN use a solid green line and for INDUS use a blue dashed line.- Set the width to 12 and the height to 8.- Change the style sheet to something you find here [Style Sheets](https://tonysyu.github.io/raw_content/matplotlib-style-gallery/gallery.html)<jupyter_code>df[['ZN', 'INDUS']].plot.line(style = ['g-', 'b--'], figsize = [12,8])
df[['ZN', 'INDUS']].plot.line(linewidth = 1, style = ['g-', 'b--'], figsize = [12,8])<jupyter_output><empty_output><jupyter_text>Bar Plots---  Bar plot from a series<jupyter_code>df['CRIM'].plot.bar(figsize = [50, 30]);<jupyter_output><empty_output><jupyter_text>Using a DataFrame and Matplotlib commands we can get fancy<jupyter_code># set the title
df['CRIM'].plot.line()
plt.title('This is a title')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend(loc = 1)<jupyter_output><empty_output><jupyter_text>Horizontal bar plot<jupyter_code>df.hist(figsize = [30,30], bins = 100);<jupyter_output><empty_output><jupyter_text>Your turn!  Create a bar chart using col1 and col2.- Give it a large title of your choosing. - Move the legend to the lower-left corner.<jupyter_code>df.mean().plot.bar(legend = 0);
plt.title('barplot');<jupyter_output><empty_output><jupyter_text>- Do the same thing but with horizontal bars.- Move the legend to the upper right.<jupyter_code>df.mean().plot.barh();<jupyter_output><empty_output><jupyter_text>We can use stacked bars<jupyter_code>df.mean().plot.bar(stacked = True);<jupyter_output><empty_output><jupyter_text>Stacked works on horizontal barcharts<jupyter_code>df.mean().plot.barh(stacked = True);<jupyter_output><empty_output><jupyter_text>Plot the `.value_counts()` of `CHAS` using a bar chart<jupyter_code>df['CHAS'].value_counts().plot.bar()<jupyter_output><empty_output><jupyter_text>Create a vertical stacked bar chart using col1 and col2- What happens when you use `df[['col1', 'col2']]` vs. `df[['col2', 'col1']]`?<jupyter_code># A:<jupyter_output><empty_output><jupyter_text>Create a horizontal stacked bar chart using all columns from df<jupyter_code>df.plot.barh(stacked = True)<jupyter_output><empty_output><jupyter_text>Histograms---<jupyter_code># A:<jupyter_output><empty_output><jupyter_text>Single historgram<jupyter_code>norm = np.random.standard_normal(5000)
plt.hist(norm);<jupyter_output><empty_output><jupyter_text>Bins param adjusts the no. of bins<jupyter_code>plt.hist(norm, bins = 100);<jupyter_output><empty_output><jupyter_text>Create a histogram with pandas for using `MEDV` in the housing data.- Set the bins to 20.<jupyter_code>df['MEDV'].plot.hist(bins = 20);<jupyter_output><empty_output><jupyter_text>Boxplots---We can use boxplots to quickly summarize distributions  Use a boxplot to preview the distributions in the housing data<jupyter_code>df.plot()
df = df.apply(stats.zscore)<jupyter_output><empty_output><jupyter_text>Scatter Plots---<jupyter_code># A:<jupyter_output><empty_output><jupyter_text>View the association between the variables `"ZN"` and `"INDUS"` using a scatter plot.<jupyter_code># A:<jupyter_output><empty_output><jupyter_text>Use a list comprehension to change the size of the scatter plot dots based on `DIS`<jupyter_code># A:<jupyter_output><empty_output><jupyter_text>Seaborn `pairplot`---With the dataframe object `housing`, we will render a pairplot using the Seaborn library.What do each of the elements represent? Is this more or less useful than the previous plot?<jupyter_code># A:<jupyter_output><empty_output><jupyter_text>Seaborn `heatmap`---When you have too many variables, a pairplot or scatter matrix can become impossible to read. We can still gauge linear correlation using a heatmap of the correlation matrix.<jupyter_code># A:<jupyter_output><empty_output><jupyter_text>Understanding figures, subplots, and axes--- Matplotlib uses a blank canvas called a figure<jupyter_code># A:<jupyter_output><empty_output><jupyter_text>Within this canvas, we can contain smaller objects called axes<jupyter_code># A:<jupyter_output><empty_output><jupyter_text>Pandas allows us to plot to a specified axes if we pass the object to the ax paramter.<jupyter_code># A:<jupyter_output><empty_output><jupyter_text>Let's use a bit more customization---<jupyter_code># We can change the ticks' size

# We can also change which ticks are visible
# list comprehension to get only the even ticks

# We can change the label rotation

# We have to use .set_title() to fix title size<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1304413772182618, 'fraction_numerical': 0.0078871530411042, 'mean_word_length': 6.973397823458283, 'pattern_counts': {'":': 0, '<': 134, '<?xml version=': 0, '>': 134, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '33', 'dir': 'jupyter-structured-clean-dedup', 'id': '522979', 'license': 'MIT', 'n_tokens_mistral': 2407, 'n_tokens_neox': 2310, 'n_words': 570, 'path': 'Intro to Data Science with Python Beginners Bootcamp Andrew August/04_visualisation_regression/explore-python-data-viz.ipynb', 'repo_name': 'alistairwalsh/p4ds', 'size': '6611'}