<jupyter_start><jupyter_text>MatplotlibMatplotlib is a popular Python library for graphical plotting. It works well with NumPy and Pandas. The gallery on Matplotlib's website shows what kinds of plotting that it can perform. There is also a good reference site on the web.<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>To show plots in Jupyter Notebook, we need to execute `%matplotlib inline`. If we use other editors, we need to run `plt.show()` at the end to have our plot show up in a window.<jupyter_code>%matplotlib inline
# Generate data
x = np.linspace(0, 5, 11)
y = x ** 2

figure = plt.figure()
axes = figure.add_axes([.1, .1, .8, .8])
axes.plot(x, y)
axes.set_xlabel('X Label')
axes.set_ylabel('Y Label')
axes.set_title('Title')
# Generate data for a sine curve between 0 and 4*pi 
sin_x = np.linspace(0, 4*np.pi, 100)
sin_y = np.sin(sin_x) 

fig = plt.figure(figsize=(8,4))
ax = fig.add_axes([0, 0, 1, 1])
ax.set_xlim(0, 4*np.pi) # 
ax.set_ylim(-1.5, 1.5)
ax.plot(sin_x, sin_y, 'g')<jupyter_output><empty_output><jupyter_text>Multiple Axes<jupyter_code>figure = plt.figure()
axes1 = figure.add_axes([0, 0, 1, 1])
axes1.set_facecolor('#d1ffb8')
axes1.plot(x, y, 'g.--')

axes2 = figure.add_axes([1.1, 0, 1, 1])
axes2.set_facecolor('#a6ddff')
axes2.plot(y, x, 'b*--')<jupyter_output><empty_output><jupyter_text>Automatic Axis ManagerInstead of creating a figure and calling `add_axes()`, we can use the `subplots()` method. It acts like an automatic axis manager.<jupyter_code># Create an empty canvas with two subplots
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(15,4))
axes[0].plot(x, y, 'g')
axes[1].plot(y, x, 'b')<jupyter_output><empty_output><jupyter_text>Overlapping FiguresA common issue with matplolib is overlapping subplots or figures. The method **plt.tight_layout()** automatically adjusts subplot parameters so that the subplot fits in to the figure canvas. This avoids overlapping content. This is an experimental feature and may not work for some cases. It only checks the extents of ticklabels, axis labels, and titles. For more information read the Tight Layout guide.<jupyter_code>fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
for ax in [ax1, ax2, ax3, ax4]:
    ax.plot(x, y, 'g')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_title('title')
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
for ax in [ax1, ax2, ax3, ax4]:
    ax.plot(x, y, 'g')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_title('title')
plt.tight_layout()
fig, axes = plt.subplots(nrows=2, ncols=2)
i = 0
for outer in axes:
    for ax in outer:
        i += 1
        ax.plot(x, y, 'g')
        ax.set_xlabel('x')
        ax.set_ylabel('y')
        ax.set_title('Axes No.{}'.format(i))
plt.tight_layout()<jupyter_output><empty_output><jupyter_text>Colours, Markers and Legends<jupyter_code>fig, ax = plt.subplots(figsize=(12,6))

ax.plot(x, x**2, label='Squared', color='purple', linewidth=2, linestyle='-.', marker='o')
ax.plot(x, x**3, label='Cubed', color='green', marker='x', markersize=12)

# Use loc=0 to let Matplotlib determine the best location
ax.legend(loc=10)<jupyter_output><empty_output><jupyter_text>Twin axesLet two or more plots share the same x-axis using `twinx()` or the same y-axis using `twiny()`.<jupyter_code>fig = plt.figure()
ax1 = fig.add_axes([0, 0, 1, 1])
ax2 = ax1.twinx() #  Note that ax2 shares the x-axis with ax1
x2 = np.linspace(0., 10., 100)

ax1.set_ylabel('Density (cgs)', color='red')
ax2.set_ylabel('Temperature (K)', color='blue')
ax1.set_xlabel('Time (s)')

ax1.plot(x2, x2 ** 2, 'b-')
ax2.plot(x2, 1000 / (x2 + 1), 'r-')<jupyter_output><empty_output><jupyter_text>Plot Types<jupyter_code>plt.scatter(x,y)
from random import sample
data = sample(range(1, 1000), 100)
plt.hist(data)
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, vert=True, patch_artist=True);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15426859710434349, 'fraction_numerical': 0.03320019970044932, 'mean_word_length': 5.145705521472393, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '152996', 'license': 'MIT', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1561, 'n_words': 428, 'path': 'matplotlib.ipynb', 'repo_name': 'sheikhomar/ml', 'size': '4006'}