import matplotlib.pyplot as pltimport numpy as npfrom regmonkey_style import stylewizard as swsw.set_templates("regmonkey_boxplot")# Generate example datanp.random.seed(42)N =50data = np.random.chisquare(1, N)data2 = np.random.normal(0, 1, N)# Create a box plotfig, ax = plt.subplots(figsize=(5, 6))ax.set_title("Box Plot of Random Data", weight='bold')ax.boxplot([data, data2], vert=True, patch_artist=True, widths=0.35)ax.set_xlabel("Category")ax.set_ylabel("Values")LOGO =""ax.add_patch( plt.Rectangle( (-0.01, 0.99), # Set location of rectangle by lower left corder0.12, # Width of rectangle-0.04, # Height of rectangle. Negative so it goes down. facecolor="#475ED1", transform=fig.transFigure, clip_on=False, linewidth=0, ))ax.text(0.05, 0.98, LOGO, # Set x, y position inside the rectangle weight='bold', color='white', # Text color fontsize=10, # Font size ha='center', # Horizontal alignment va='top', # Vertical alignment transform=fig.transFigure) # Relative to the figureax.plot( [-0.01, 0.9], # Set width of line [0.99, 0.99], # Set height of line transform=fig.transFigure, # Set location relative to plot clip_on=False, color="#475ED1", linewidth=0.6,)plt.show()
import numpy as npimport polars as plnp.random.seed(42)def stem_and_leaf(data, leaf_scale=1):# Sort the data data =sorted(data)# Create a dictionary to hold the stems and leaves stem_leaf = {}for number in data:# Split the number into stem and leaf based on the leaf scale stem = number // (10** leaf_scale) leaf = number % (10** leaf_scale)# Add the leaf to the appropriate stemif stem in stem_leaf: stem_leaf[stem].append(leaf)else: stem_leaf[stem] = [leaf]# Print the headerprint(f"{'Freq':>4} | {'Stem':>4} | {'Leaf':<20} | {'CumFreq':>7}")print('-'*40)# Print the stem-and-leaf plot with frequencies and cumulative frequencies cumulative_freq =0for stem, leaves insorted(stem_leaf.items()): frequency =len(leaves) cumulative_freq += frequency leaves_str =' '.join(f"{leaf:0{leaf_scale}d}"for leaf in leaves)print(f"{frequency:>4} | {stem:>4} | {leaves_str:<20} | {cumulative_freq:>7}")# Example datadata = np.random.randint(0, 100, 21)data.sort()print("sorted data is \n{}\n".format(data))stem_and_leaf(data, leaf_scale=1)print(pl.DataFrame({'x':data}).describe())