<jupyter_start><jupyter_text>Introduction to Matplotlib<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
plt.plot()
# The semicolon prevents the [] from printing also
plt.plot();
plt.plot()
plt.show()
plt.plot([1,2,3,4]);
x = [1, 2, 3, 4]
y = [11, 22, 33, 44]
plt.plot(x, y);
# 1st method
fig = plt.figure() # creates a figure
ax = fig.add_subplot() # adds some axes
plt.show()
# 2nd method
fig = plt.figure() # creates a figure
ax = fig.add_axes([1, 1, 1, 1])
ax.plot(x, y) # add some data
plt.show()
# 3rd method (recommended)
fig, ax = plt.subplots()
ax.plot(x, y); # add some data
type(fig), type(ax)<jupyter_output><empty_output><jupyter_text>Matplotlib example workflow<jupyter_code># 0. import matplotlib and get it ready for plotting in Jupyter
%matplotlib inline
import matplotlib.pyplot as plt

# 1. Prepare data
x = [1, 2, 3, 4]
y = [11, 22, 33, 44]

# 2. Setup plot
fig, ax = plt.subplots(figsize=(10,10)) # (width, height)

# 3. Plot the data
ax.plot(x, y)

# 4. Customize plot
ax.set(title="Simple Plot",
       xlabel="x-axis",
       ylabel="y-axis")

# 5. Save & show (you save the whole figure)
fig.savefig("sample-plot.png")<jupyter_output><empty_output><jupyter_text>Making figures with Numpy arraysWe want:* Line plot* Scatter plot* Bar plot* Histogram* Subplots<jupyter_code>import numpy as np
# Create some data
x = np.linspace(0, 10, 100)
x[:10]
# Plot the data and create a line plot
fig, ax = plt.subplots()
ax.plot(x, x**2)
# Use the same data to make a scatter
fig, ax = plt.subplots()
ax.scatter(x, np.exp(x));
# Another scatter plot
fig, ax = plt.subplots()
ax.scatter(x, np.sin(x));
# Make a plot from dictionary
nut_butter_prices = {"Almond butter": 10,
                     "Peanut butter": 8,
                     "Cashew butter": 12}
fig, ax = plt.subplots()
ax.bar(nut_butter_prices.keys(), nut_butter_prices.values())
ax.set(title="Dan's Nut Butter Store", 
      ylabel="Price ($)");
fig, ax=plt.subplots()
ax.barh(list(nut_butter_prices.keys()), list(nut_butter_prices.values()));
# Make some data for histograms and plot it
x = np.random.randn(1000)
fig, ax=plt.subplots()
ax.hist(x);<jupyter_output><empty_output><jupyter_text>Two options for subplots<jupyter_code># Subplot option 1
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2,
                                             ncols=2,
                                             figsize=(10, 10))
# Plot to each different axis
ax1.plot(x, x/2);
ax2.scatter(np.random.random(10), np.random.random(10));
ax3.bar(nut_butter_prices.keys(), nut_butter_prices.values());
ax4.hist(np.random.randn(1000));<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1496268656716418, 'fraction_numerical': 0.03246268656716418, 'mean_word_length': 3.955637707948244, 'pattern_counts': {'":': 3, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '6572', 'license': 'MIT', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1001, 'n_words': 273, 'path': 'Introduction to Matplotlib.ipynb', 'repo_name': 'DonAvery/my-ds-ml-education-journey', 'size': '2680'}