<jupyter_start><jupyter_text>PlottingThese problems go along with the material presented in the `Basic Plotting` notebook in the python prep folder. Basic Problems1. Import `matplotlib` (note not just `matplotlib.pyplot`) what version do you have?2. Import `matplotlib.pyplot` as `plt`.3. Plot the line $y = 4x+2$ using `plt.plot()`.4. Plot the random walk you saved to file in notebook 4.<jupyter_code>## 1. ANSWER

import matplotlib

print(matplotlib.__version__)


## 2. ANSWER

import matplotlib.pyplot as plt
## 3. ANSWER

# make x and y
x = range(-10,11,1)
y = [4*i + 2 for i in x]

# make the figure, I specified a figsize, you don't need to
plt.figure(figsize = (8,6))

# plot y against x
plt.plot(x,y)

# add labels
plt.xlabel("x", fontsize=16)
plt.ylabel("y", fontsize=16)

# show the plot
plt.show()
## 4. ANSWER
import pandas as pd
## 4. ANSWER cont.
random_walk = pd.read_csv("random_walk.csv")

random_walk.head()
## 4. ANSWER cont.
# make the figure, I specified a figsize, you don't need to
plt.figure(figsize = (8,6))

# plot y against x
plt.plot(random_walk.index, random_walk.position)

# add labels
plt.xlabel("Step", fontsize=16)
plt.ylabel("Position", fontsize=16)

plt.title("A Random Walk", fontsize=18)

# show the plot
plt.show()<jupyter_output><empty_output><jupyter_text>More Advanced Problems1. Plot two random normal histograms using `np.random.randn` to generate the random normal draws. Make one red and one blue, add a legend.2. Write a function that takes in $n$ then plots a random walk of length $n$. You may want to give the user the ability to choose the color and `linewidth` of their walk.3. Load the `iris.csv` data using `pandas`. Make a 2 x 2 subplot. In the (0,0) subplot plot the `sepal_length` on the $x$ and the `sepal_width` on the $y$, in the (1,0) subplot plot the `petal_length` on the $x$ and the `sepal_width` on the $y$, and finally in the (0,1) subplot plot the `sepal_length` on the $x$ and the `petal_width` on the $y$. Color each point by its `target`. Write the text "Iris Plot" in the (1,1) subplot.<jupyter_code>## 1. ANSWER
import numpy as np
## 1. ANSWER continued

plt.figure(figsize=(8,6))

plt.hist(np.random.randn(100), color = "red", label="Draw 1")
plt.hist(np.random.randn(100) + 2, color = "blue", label="Draw 2")

plt.legend(fontsize=14)

plt.show()
## 2. ANSWER

# from notebook 5 solution
def random_walk(N):
    # get the flips
    flips = np.random.binomial(1,p=.5,size=N)
    flips = 2*flips - 1

    # Get the positions
    pos = np.zeros(N+1)
    pos[1:] = np.cumsum(flips)
    
    # return the positions
    return pos

def plot_walk(N, color="blue", linewidth=1.5):
    # get the walk
    walk = random_walk(N)
    
    # make the figure
    plt.figure(figsize=(8,6))
    
    # need to add 1 to N for range to account for the 
    # 0 position
    plt.plot(range(N+1), walk, linewidth = linewidth, color = color)
    
    plt.xlabel("Step", fontsize=16)
    plt.ylabel("Position", fontsize=16)
    
    plt.title("A Random Walk", fontsize=20)
    
    plt.show()
## 2. ANSWER cont.

plot_walk(100)

plot_walk(1000, linewidth=1, color='orange')
## 3. ANSWER
iris = pd.read_csv("iris.csv")


iris.head()
## 3. ANSWER cont.

fig, ax = plt.subplots(2,2, figsize=(12,12))

ax[0,0].scatter(iris.loc[iris.target==0].sepal_length, 
                iris.loc[iris.target==0].sepal_width,
               c='r', label="setosa")
ax[0,0].scatter(iris.loc[iris.target==1].sepal_length, 
                iris.loc[iris.target==1].sepal_width,
               c='b', label="versicolor")
ax[0,0].scatter(iris.loc[iris.target==2].sepal_length, 
                iris.loc[iris.target==2].sepal_width,
               c='g', label="virginica")
ax[0,0].set_ylabel("Sepal Width", fontsize=16)
ax[0,0].set_xlabel("Sepal Length", fontsize=16)
ax[0,0].legend(fontsize=12)

ax[1,0].scatter(iris.loc[iris.target==0].petal_length, 
                iris.loc[iris.target==0].sepal_width,
               c='r', label="setosa")
ax[1,0].scatter(iris.loc[iris.target==1].petal_length, 
                iris.loc[iris.target==1].sepal_width,
               c='b', label="versicolor")
ax[1,0].scatter(iris.loc[iris.target==2].petal_length, 
                iris.loc[iris.target==2].sepal_width,
               c='g', label="virginica")
ax[1,0].set_ylabel("Sepal Width", fontsize=16)
ax[1,0].set_xlabel("Petal Length", fontsize=16)
ax[1,0].legend(fontsize=12)

ax[0,1].scatter(iris.loc[iris.target==0].sepal_length, 
                iris.loc[iris.target==0].petal_width,
               c='r', label="setosa")
ax[0,1].scatter(iris.loc[iris.target==1].sepal_length, 
                iris.loc[iris.target==1].petal_width,
               c='b', label="versicolor")
ax[0,1].scatter(iris.loc[iris.target==2].sepal_length, 
                iris.loc[iris.target==2].petal_width,
               c='g', label="virginica")
ax[0,1].set_ylabel("Petal width", fontsize=16)
ax[0,1].set_xlabel("Sepal Length", fontsize=16)
ax[0,1].legend(fontsize=12)

ax[1,1].text(0.4,0.45, "Iris", fontsize=20)

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15629322268326418, 'fraction_numerical': 0.03576368306658763, 'mean_word_length': 3.606005459508644, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '484679', 'license': 'ECL-2.0', 'n_tokens_mistral': 2040, 'n_tokens_neox': 1916, 'n_words': 462, 'path': 'python prep/Practice Problems/7. Plotting - Solutions.ipynb', 'repo_name': 'MattGraham1996/Erdos-Institute', 'size': '5061'}