<jupyter_start><jupyter_text>Imports<jupyter_code>from matplotlib import pyplot as plt
from matplotlib import rcParams
%matplotlib inline

import numpy as np<jupyter_output><empty_output><jupyter_text>Default Parameters<jupyter_code>figsize = (4, 5) # in inches
fontsize = 10
save_dpi = 300
linewidth = 0.5

rcParams.update({
    'font.size' : fontsize,
    'savefig.dpi' : 300,
    'axes.linewidth' : 0.5,
})<jupyter_output><empty_output><jupyter_text>Data<jupyter_code># data
x = ['cat', 'dog', 'fly', 'mouse', 'human']
y1 = [3, 2, -1, 3, 7]
y2 = [5, 10, -2, 4, 3]

# error
ey1 = [0.5, 0.2, 1, 0.3, 2]
ey2 = [1.5, 0.6, 2, 1.3, 1]

# labeling
ylabel1 = 'units_1'
ylabel2 = 'units_2'

# coloring
colors = ['#edf8fb',
          '#b2e2e2',
          '#66c2a4',
          '#2ca25f',
          '#006d2c']<jupyter_output><empty_output><jupyter_text>Beginner plot this<jupyter_code># create figure and axis
'''
    hint - use the imported pyplot (plt) command to create a figure
    typically you can go about this using the subplots(rows, columns, figsize) function
    which gives you both the figure, and an array of axes
    or you can create the figure first using figure(), then add axes onto it using add_axes()
    or add_subplot() (not the lack of an 's')
    
    example 
    fig, axes = plt.subplots(2, 1, figsize=figsize)
    # note that axes is an array containing TWO axis. One on top of the other (2 rows with 1 column)
    # To directly effect the AXIS (not AXES), you need to use indexing
    ax1 = axes[0]
'''

# set up bar spacing
# plot a bar chart per axis
'''
    hint - mostly all 2D charts need an X and Y in the form of numerical values.
    You need to convert the X values to numbers, say, 0, 1, 2, 3, ... per index. Use the np.arange(N) function
    
    hint - a unique aspect of bar charts is that you can control their spacing (X values) and their widths
    Use the width option in ax.bar(... width=width) to set the spacing. 
    Read the ax.bar() API to see what other parameters you can adjust, such as edgecolor.
    https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.bar.html
    
    example - 
    ax1.bar(X, Y, width=width, edgecolor='k') # k stands for black
'''        

# give them x and y labels
# change limits
'''
    hint - set setters to set the xticks and xlabels, ylabels, and limits of the plot
    
    example - 
    ax1.set_ylabel(<args>)
    ax1.set_ylim(<args>)
    ax1.set_xticklabels(<args>) ...
'''<jupyter_output><empty_output><jupyter_text>Advanced plot this<jupyter_code># create figure and axis

# set up bar spacing
# plot a bar chart per axis

# use a for loop to change both the first and second axis, without having to repeat code
'''
    example -
    for i, ax, y, e, label in zip(np.arange(len(axes)), axes, [y1, y2], [ey1, ey2], [ylabel1, ylabel2]):
    # the zip function packages similarly sized arrays (or lists) and indexes through each one simultaneously
    # so the first iteration contains
    # i=0, ax=ax1, y=y1, e=ey1, label=ylabel1
    # second iteration contains
    # i=1, ax=ax2, y=y2, e=ey2, label=ylabel2
'''

# within the for loop, plot the bar chart

# move the spines so they look less awkward
'''
hint - use the ax.spines command to move around the axis borders (i.e. spines)

example - 
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
'''
    
# plot the errorbars
'''
hint - use the errobar command ax.errorbar()
find the relevant input arguments in the documentation
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.errorbar.html
'''

# use setters to change xtick and xlabels, ylabels, ylimits, figlabel, etc.<jupyter_output><empty_output><jupyter_text>Save<jupyter_code>'''
    - example
    fig.savefig('title', dpi=300, transparent=True)
'''<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11825996353216983, 'fraction_numerical': 0.025527481114873664, 'mean_word_length': 3.5988023952095807, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '604428', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1328, 'n_words': 461, 'path': 'exercise2-bar_chart/exercise-bar_chart.ipynb', 'repo_name': 'MIT-BECL/python-plotting-notes', 'size': '3839'}