<jupyter_start><jupyter_text>Matplotlib Advanced ExamplesThis notebook is the compilation of code from the git repo by [brohrer/taming_matplotlib](https://github.com/brohrer/taming_matplotlib).  just_plot_it<jupyter_code>import numpy as np
import matplotlib.pyplot as plt


x_curve = np.linspace(-2 * np.pi, 2 * np.pi, 500)
y_curve = np.sinc(x_curve)

plt.figure()
plt.plot(x_curve, y_curve)
#plt.savefig("images/line_plot.png")
plt.show()

x_scatter = np.linspace(-1, 1)
y_scatter = x_scatter + np.random.normal(size=x_scatter.size)

plt.figure()
plt.scatter(x_scatter, y_scatter)
#plt.savefig("images/scatter_plot.png")
plt.show()<jupyter_output><empty_output><jupyter_text>lines_examples<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

# Create the curve data.
x = np.linspace(-6, 6, 500)
y = np.sinc(x)

# Vary the line width.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y, linewidth=15)
#fig.savefig("images/wide_line.png")

# Vary the line color.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y, color="pink", linewidth=5)
#fig.savefig("images/pink_line.png")

# Vary the line style.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y, linewidth=2, linestyle="--")
#fig.savefig("images/dashed_line.png")

# Make multiple lines.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y, linewidth=4)
ax.plot(x, y + .1, linewidth=2)
ax.plot(x, y + .2, linewidth=1)
ax.plot(x, y + .3, linewidth=.5)
ax.plot(x, y + .4, linewidth=.2)
#fig.savefig("images/multiple_lines.png")

# Vary the joint style and cap style.
fig = plt.figure()
ax = fig.gca()
ax.plot(
    [.3, .4, .8],
    [.2, .4, .1],
    linewidth=26,
    solid_capstyle="projecting",
    solid_joinstyle="bevel",
)
ax.plot(
    [.25, .35, .75],
    [.4, .6, .3],
    linewidth=26,
    solid_capstyle="round",
    solid_joinstyle="round",
)
ax.plot(
    [.2, .3, .7],
    [.6, .8, .5],
    linewidth=26,
    solid_capstyle="butt",
    solid_joinstyle="miter",
)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
#fig.savefig("images/cap_join_styles.png")<jupyter_output><empty_output><jupyter_text>patch_examples<jupyter_code>import matplotlib.patches as patches
import matplotlib.pyplot as plt


path = [
    [.1, .3],
    [.2, .9],
    [.8, .4],
]
fig = plt.figure()
ax = fig.gca()
ax.add_patch(patches.Polygon(path))
#fig.savefig("triangle_patch.png", dpi=150)
plt.close()

path = [
    [.1, .3],
    [.2, .9],
    [.8, .4],
]
fig = plt.figure()
ax = fig.gca()
ax.add_patch(patches.Polygon(
  path,
  alpha=.7,
  edgecolor="darkblue",
  facecolor="red",
  hatch="+",
  joinstyle="miter",
  linestyle="--",
  linewidth=5,
))
#fig.savefig("fancy_patch.png", dpi=150)
#plt.close()<jupyter_output><empty_output><jupyter_text>points_examples<jupyter_code>import matplotlib.pyplot as plt
import numpy as np


# Create the point data.
x = np.linspace(-1, 1)
y = x + np.random.normal(size=x.size)

# Vary the marker size.
fig = plt.figure()
ax = fig.gca()
ax.scatter(x, y, s=80)
#fig.savefig("images/markers_large.png")

# Make markers different sizes
fig = plt.figure()
ax = fig.gca()
sizes = (np.random.sample(size=x.size) * 10) ** 2
ax.scatter(x, y, s=sizes)
#fig.savefig("images/markers_sized.png")

# Vary the marker style.
fig = plt.figure()
ax = fig.gca()
ax.scatter(x, y, marker="v")
#fig.savefig("images/markers_triangle.png")

# Make markers of different styles.
fig = plt.figure()
ax = fig.gca()
N = x.size // 3
ax.scatter(x[:N], y[:N], marker="o")
ax.scatter(x[N: 2 * N], y[N: 2 * N], marker="x")
ax.scatter(x[2 * N:], y[2 * N:], marker="s")
#fig.savefig("images/markers_styled.png")

# Change the marker colors
fig = plt.figure()
ax = fig.gca()
ax.scatter(x, y, c="orange")
#fig.savefig("images/markers_orange.png")

# Vary the marker colors.
fig = plt.figure()
ax = fig.gca()
ax.scatter(x, y, c=x - y)
#fig.savefig("images/markers_colors.png")

# Create a lot of point data.
x = np.linspace(-1, 1, num=100000)
y = x + np.random.normal(size=x.size)

# Make markers transparent.
fig = plt.figure()
ax = fig.gca()
ax.scatter(x, y, marker=".", alpha=.05, edgecolors="none")
#fig.savefig("images/markers_transparent.png")<jupyter_output><empty_output><jupyter_text>text_examples<jupyter_code>import matplotlib
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline


# Create the curve data.
x = np.linspace(-6, 6, 500)
y = np.sinc(x)

# Add axis labels and a title.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)
ax.set_xlabel("The x-axis label", fontsize=16)
ax.set_ylabel("The y-axis label", fontsize=16)
ax.set_title("The Title", fontsize=18)
#fig.savefig("images/text_xy_labels_title.png")
plt.show()

# Add some text.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)
ax.text(-5, .8, "Some text", fontsize=22)
#fig.savefig("images/text_any_text.png")
plt.show()

# Align the text.
fig = plt.figure()
ax = fig.gca()
ax.set_xlim([-2, 2])
ax.set_ylim([-1.4, 1.4])
left_pos = -1.8
right_pos = 1.8
top_pos = 1
bottom_pos = -1
center_pos = 0
baseline_pos = -.5
fontsize = 14
ax.scatter(left_pos, bottom_pos, marker=".", c="black")
ax.text(left_pos, bottom_pos, "left-bottom",
        fontsize=fontsize,
        horizontalalignment="left",
        verticalalignment="bottom")
ax.scatter(right_pos, bottom_pos, marker=".", c="black")
ax.text(right_pos, bottom_pos, "right-bottom",
        fontsize=fontsize,
        horizontalalignment="right",
        verticalalignment="bottom")
ax.scatter(left_pos, top_pos, marker=".", c="black")
ax.text(left_pos, top_pos, "left-top",
        fontsize=fontsize,
        horizontalalignment="left",
        verticalalignment="top")
ax.scatter(right_pos, top_pos, marker=".", c="black")
ax.text(right_pos, top_pos, "right-top",
        fontsize=fontsize,
        horizontalalignment="right",
        verticalalignment="top")
ax.scatter(center_pos, bottom_pos, marker=".", c="black")
ax.text(center_pos, bottom_pos, "center-bottom",
        fontsize=fontsize,
        horizontalalignment="center",
        verticalalignment="bottom")
ax.scatter(center_pos, top_pos, marker=".", c="black")
ax.text(center_pos, top_pos, "center-top",
        fontsize=fontsize,
        horizontalalignment="center",
        verticalalignment="top")
ax.scatter(right_pos, center_pos, marker=".", c="black")
ax.text(right_pos, center_pos, "right-center",
        fontsize=fontsize,
        horizontalalignment="right",
        verticalalignment="center")
ax.scatter(left_pos, center_pos, marker=".", c="black")
ax.text(left_pos, center_pos, "left-center",
        fontsize=fontsize,
        horizontalalignment="left",
        verticalalignment="center")
ax.scatter(right_pos, baseline_pos, marker=".", c="black")
ax.text(right_pos, baseline_pos, "right-baseline",
        fontsize=fontsize,
        horizontalalignment="right",
        verticalalignment="baseline")
ax.scatter(left_pos, baseline_pos, marker=".", c="black")
ax.text(left_pos, baseline_pos, "left-baseline",
        fontsize=fontsize,
        horizontalalignment="left",
        verticalalignment="baseline")
ax.scatter(center_pos, baseline_pos, marker=".", c="black")
ax.text(center_pos, baseline_pos, "center-baseline",
        fontsize=fontsize,
        horizontalalignment="center",
        verticalalignment="baseline")
ax.scatter(center_pos, center_pos, marker=".", c="black")
ax.text(center_pos, center_pos, "center-center",
        fontsize=fontsize,
        horizontalalignment="center",
        verticalalignment="center")
ax.set_title("Text alignment options", fontsize=18)
#fig.savefig("images/text_alignment.png")
plt.show()

# Change the text formatting. 
fig = plt.figure()
ax = fig.gca()
ax.set_xlim([-2, 2])
ax.set_ylim([-2, 2])
fontsize = 16
ax.text(-1, 1, 'fontsize=20', fontsize=20,
        horizontalalignment="center")
ax.text(-1, 0, 'fontstyle="italic"', fontstyle="italic",
        fontsize=fontsize,
        horizontalalignment="center")
ax.text(-1, -1, 'fontweight="bold"', fontweight="bold",
        fontsize=fontsize,
        horizontalalignment="center")
ax.text(1, -1, "rotation=15", rotation=15,
        fontsize=fontsize,
        horizontalalignment="center")
ax.text(1, 0, 'color="red"', color="red",
        fontsize=fontsize,
        horizontalalignment="center")
ax.text(1, 1, 'family="serif"', family="serif",
        fontsize=fontsize,
        horizontalalignment="center")
ax.set_title("A sampling of text styling options", fontsize=18)
#fig.savefig("images/text_styles.png")
plt.show()

# Text box
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)
ax.text(1, .7, "Text in a box",
        fontsize=20,
        bbox=dict(boxstyle = "square",
                  facecolor = "white"))
ax.set_title("A bounding box", fontsize=18)
#fig.savefig("images/text_bbox.png")
plt.show()

# Annotation
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)
fontsize = 20
ax.annotate(
    '"-"',
    (2.4, np.sinc(2.4)),
    (3, .4),
    arrowprops=dict(arrowstyle = "-"),
    fontsize=fontsize,
)
ax.annotate(
    '"-|>"',
    (-2.4, np.sinc(2.4)),
    (-5, .3),
    arrowprops=dict(arrowstyle = "-|>"),
    fontsize=fontsize,
)
ax.annotate(
    '"<->"',
    (.5, np.sinc(.5)),
    (3, .6),
    arrowprops=dict(arrowstyle = "<->"),
    fontsize=fontsize,
)
ax.annotate(
    '"simple"',
    (0, 1),
    (-6, .7),
    arrowprops=dict(arrowstyle = "simple"),
    fontsize=fontsize,
)
ax.annotate(
    '"fancy"',
    (-.7, np.sinc(.7)),
    (-6, .55),
    arrowprops=dict(arrowstyle = "fancy"),
    fontsize=fontsize,
)
ax.set_title("A few arrowstyle options", fontsize=18)
#fig.savefig("images/text_annotate.png")
plt.show()

# LaTeX equations. 
fig = plt.figure()
ax = fig.gca()
ax.set_xlim([-1, 1])
ax.set_ylim([-1, 1])
matplotlib.rcParams['text.usetex'] = True
ax.text(
    0, 0,
    r"$f(x | \mu, \sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}}" +
    r"e^{- \frac{(x - \mu)^2}{2\sigma^2}}$",
    horizontalalignment="center",
    fontsize=24)
matplotlib.rcParams['text.usetex'] = False
ax.set_title("So much math", fontsize=18)
#fig.savefig("images/text_equation.png")
plt.show()

fig = plt.figure()
ax = fig.gca()
ax.set_xlim([-1, 1])
ax.set_ylim([-1, 1])
matplotlib.rcParams['text.usetex'] = True
ax.text(
    0, 0, r"$E=mc^2$",
    horizontalalignment="center",
    fontsize=32)
matplotlib.rcParams['text.usetex'] = False
ax.set_title("Some math", fontsize=18)
#fig.savefig("images/text_equation_thumbnail.png")
plt.show()<jupyter_output><empty_output><jupyter_text>tick_examples<jupyter_code>import matplotlib
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline


x = np.linspace(1, 13, 500)
y = 1 + np.sinc(x - 7)

# Set axis limits.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)
ax.set_xlim(4, 10)
ax.set_ylim(-1, 3)
ax.set_xlabel("x-axis limits set at 4 and 10", fontsize=16)
ax.set_ylabel("y-axis limits set at -1 and 3", fontsize=16)
#fig.savefig("images/axis_limits.png")
plt.show()
# Change to log scaling.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel("log scale x-axis", fontsize=16)
ax.set_ylabel("log scale y-axis", fontsize=16)
#fig.savefig("images/axis_log.png")
plt.show()
# Change tick direction.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)
ax.tick_params(axis="x", direction="in")
ax.tick_params(axis="y", direction="inout")
ax.set_xlabel("inward x-axis ticks", fontsize=16)
ax.set_ylabel("bi-directional y-axis ticks", fontsize=16)
#fig.savefig("images/axis_tick_direction.png")
plt.show()
# Draw opposite ticks and labels.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)
ax.tick_params(bottom=False, top=True, left=True, right=True)
ax.tick_params(labelbottom=False, labeltop=True, labelleft=True, labelright=True)
ax.set_xlabel("x-axis ticks on top", fontsize=16)
ax.set_ylabel("y-axis ticks on both sides", fontsize=16)
#fig.savefig("images/axis_tick_opposite.png")
plt.show()
# Style ticks.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)
ax.tick_params(axis="x", direction="in", length=16, width=2, color="turquoise")
ax.tick_params(axis="y", direction="in", length=6, width=4, color="orange")
ax.set_xlabel("long turquoise x-axis ticks", fontsize=16)
ax.set_ylabel("short orange y-axis ticks", fontsize=16)
#fig.savefig("images/axis_tick_styled.png")
plt.show()
# Style tick labels.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)
ax.tick_params(axis="x", labelsize=18, labelrotation=-60, labelcolor="turquoise")
ax.tick_params(axis="y", labelsize=12, labelrotation=20, labelcolor="orange")
ax.set_xlabel("big turquoise x-axis ticks", fontsize=16)
ax.set_ylabel("small orange y-axis ticks", fontsize=16)
#fig.savefig("images/axis_tick_label_styled.png")
plt.show()
# Minor ticks.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)

from matplotlib.ticker import AutoMinorLocator
x_minor_locator = AutoMinorLocator()
ax.xaxis.set_minor_locator(x_minor_locator)
y_minor_locator = AutoMinorLocator()
ax.yaxis.set_minor_locator(y_minor_locator)
ax.set_xlabel("minor x-axis ticks", fontsize=16)
ax.set_ylabel("minor y-axis ticks", fontsize=16)
#fig.savefig("images/axis_tick_minor.png")
plt.show()
# Minor tick labels.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)

from matplotlib.ticker import AutoMinorLocator, FormatStrFormatter
formatter = FormatStrFormatter("%.3f")
x_minor_locator = AutoMinorLocator()
ax.xaxis.set_minor_locator(x_minor_locator)
y_minor_locator = AutoMinorLocator()
ax.yaxis.set_minor_locator(y_minor_locator)
ax.yaxis.set_minor_formatter(formatter)

ax.set_xlabel("minor x-axis ticks", fontsize=16)
ax.set_ylabel("minor y-axis ticks with labels", fontsize=16)
#fig.savefig("images/axis_tick_labels_minor.png")
plt.show()
# Control your ticks.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)

from matplotlib.ticker import FixedLocator, FixedFormatter
x_formatter = FixedFormatter([
    "I like this spot",
    "and this",
    "and that",
    "but not this one."])
x_locator = FixedLocator([3, 7, 8.8, 12])
ax.xaxis.set_major_formatter(x_formatter)
ax.xaxis.set_major_locator(x_locator)

y_formatter = FixedFormatter(["-1e7", "111", "007", "xkcd"])
y_locator = FixedLocator([.85, 1.15, 1.28, 1.9])
ax.yaxis.set_major_formatter(y_formatter)
ax.yaxis.set_major_locator(y_locator)
ax.set_xlabel("custom x-axis ticks", fontsize=16)
ax.set_ylabel("custom y-axis ticks", fontsize=16)
#fig.savefig("images/axis_tick_custom.png")
plt.show()
# Make a grid.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)
ax.grid(
    axis="x",
    color="green",
    alpha=.3,
    linewidth=2,
    linestyle=":")
ax.grid(
    axis="y",
    color="black",
    alpha=.5,
    linewidth=.5)
ax.set_xlabel("Dotted green x gridllines", fontsize=16)
ax.set_ylabel("Thin black y gridlines", fontsize=16)
#fig.savefig("images/axis_tick_grid.png")
plt.show()<jupyter_output><empty_output><jupyter_text>framing_examples<jupyter_code>import matplotlib
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline


x = np.linspace(-6, 6, 500)
y = np.sinc(x)

# Manually set Axes by size and position.
fig = plt.figure()
ax = fig.add_axes((.2, .4, .4, .4))
ax.plot(x, y)
#fig.savefig("images/framing_manual.png")
plt.show()
# Add multiple Axes.
fig = plt.figure()
ax1 = fig.add_axes((.1, .1, .35, .35))
ax1.plot(x, y)
ax2 = fig.add_axes((.1, .6, .35, .35))
ax2.plot(x, y)
ax3 = fig.add_axes((.55, .1, .35, .35))
ax3.plot(x, y)
ax4 = fig.add_axes((.55, .6, .35, .35))
ax4.plot(x, y)
ax5 = fig.add_axes((.35, .83, .08, .08))
ax5.plot(x, y)
ax6 = fig.add_axes((.75, .43, .23, .23))
ax6.plot(x, y)
ax7 = fig.add_axes((.35, -.15, .3, .3))
ax7.plot(x, y)
#fig.savefig("images/framing_multiple.png")
plt.show()
# Fill figure with Axes.
fig = plt.figure()
ax1 = fig.add_axes((0, 0, 1, 1))
ax1.plot(x, y)
#fig.savefig("images/framing_no_border.png")
plt.show()
# https://matplotlib.org/api/spines_api.html
# Turn axis spines off.
fig = plt.figure()
ax = fig.gca()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.plot(x, y)
#fig.savefig("images/framing_spines_off.png")
plt.show()
# Modify spine styles.
fig = plt.figure()
ax = fig.gca()
ax.spines['top'].set_color("orange")
ax.spines['left'].set_bounds(-.5, .5)
ax.spines['right'].set_linestyle("--")
ax.spines['bottom'].set_linewidth(6)
ax.spines['bottom'].set_capstyle("round")
ax.plot(x, y)
#fig.savefig("images/framing_spines_styled.png")
# Move spines.
fig = plt.figure()
ax = fig.gca()
ax.spines['top'].set_position(("outward", 15))
ax.spines['bottom'].set_position(("data", 0))
ax.spines['left'].set_position(("axes", .3))
ax.spines['right'].set_position(("outward", -40))
ax.plot(x, y)
#fig.savefig("images/framing_spines_moved.png")
# Change figure size.
fig = plt.figure(figsize=(4, 2))
ax = fig.gca()
ax.plot(x, y)
#fig.savefig("images/framing_small_fig.png")
# Change figure resolution.
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)
#fig.savefig("images/framing_lo_res_fig.png", dpi=15)
# Change the color of the plot area.
fig = plt.figure()
ax = fig.gca()
ax.set_facecolor("#e1ddbf")
ax.plot(x, y)
#fig.savefig("images/framing_axes_bg_color.png")
# Change the color of the figure background.
fig = plt.figure(facecolor="#e1ddbf")
ax = fig.gca()
ax.plot(x, y)
#fig.savefig("images/framing_fig_bg_color.png", facecolor=fig.get_facecolor())
# Change the color of the figure frame.
fig = plt.figure(linewidth=10, edgecolor="#04253a")
ax = fig.gca()
ax.plot(x, y)
#fig.savefig("images/framing_fig_frame.png", edgecolor=fig.get_edgecolor())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18334197338862968, 'fraction_numerical': 0.02424975519843327, 'mean_word_length': 4.6775670372792675, 'pattern_counts': {'":': 1, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 2, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '166879', 'license': 'MIT', 'n_tokens_mistral': 7223, 'n_tokens_neox': 6913, 'n_words': 1325, 'path': 'Python Basic Tutorials/Matplotlib Formatting Examples.ipynb', 'repo_name': 'iamstarstuff/PhysicStuff', 'size': '17361'}