<jupyter_start><jupyter_text>Swarm Plots Lesson *<NAME>, <NAME>, <NAME>* Our Resources:[Seaborn Blog Post](https://datasoups.blogspot.com/2018/10/seaborn-stripswarm-violin-plots.html])[Alex Gude Blog Post](https://alexgude.com/blog/distribution-plots/:~:text=Swarm%20Plots%2C%20also%20called%20beeswarm,instead%20of%20adding%20random%20jitter.)[Broadway Dataset](https://corgis-edu.github.io/corgis/csv/broadway/) What are Strip and Swarm plots and what are they used for?Strip and swarm (or beeswarm) plots are two types of visualization plots that focus on the distribution of a dataset. Both plots depict the points in the data set between a categorical and numeric variables. The x-axis contains the different categories within the categorical variable and the y-axis presents a scale for the numeric variable. The overall image can seem like a combination between a histogram and a scatter plot, with hundreds of points displaying an image of the dataset's distribution. How do we use Strip and Swarm plots?First, we need to import the necessary libraries (Pandas, Seaborn, MatPlotLib and NumPy):<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Then, load the data. We will be using the [Broadway](https://corgis-edu.github.io/corgis/csv/broadway/) dataset from the CORGIS collection. The comment below contains the different columns within the dataset and their descriptions. We renamed the column names underneath and shortened the dataset to only contain the first 1000 rows in order to speed up plot generation.<jupyter_code>'''
Date.Day	Integer	The day of the month that this performance's week ended on.	26
Date.Full	String	The full date representation that this performance's week ended on in "Month/Day/Year" format.	"8/26/1990"
Date.Month	Integer	The numeric month that this performance's week ended in (1 = January, 2 = February, etc.).	8
Date.Year	Integer	The year that this week of performances occurred in.	1990
Show.Name	String	The name of the production.	"Tru"
Show.Theatre	String	The name of the theatre.	"Booth"
Show.Type	String	Whether it is a "Musical", "Play", or "Special".	"Play"
Statistics.Attendance	Integer	The total number of people who attended performances over the week.	5500
Statistics.Capacity	Integer	The percentage of the theatre that was filled during that week.	88
Statistics.Gross	Integer	The "Gross Gross" of this performance, or how much it made in total across the entire week. Measured in dollars.	134456
Statistics.Gross Potential	Integer	The Gross Potential is the maximum amount an engagement can possibly earn based on calculations involving ticket prices, seating capacity, and the number of performances. This number is expressed here as a percentage of what could have been achieved (Gross Gross / Gross Potential). In case the GP could not be calculated, it was replaced with 0%.	0
Statistics.Performances
'''


broadway_url = "https://raw.githubusercontent.com/corgis-edu/corgis/master/source/broadway/broadway-corgis.csv"
#Renamed columns and reading only first 1000 rows
df = pd.read_csv(broadway_url, names=['Day', 'Date_Full', 'Month', 'Year', 'Name', 'Theatre', 'Type', 'Attendance', 'Capacity', 'Gross', 'Gross Potential', 'Performance'], nrows=1000)
#dimensions
df.shape
df.head(10)<jupyter_output><empty_output><jupyter_text>Now to use the stripplot and swarmplot functions from seaborn. We based our example off of the [Seaborn blog post](https://datasoups.blogspot.com/2018/10/seaborn-stripswarm-violin-plots.html]) which is also linked above in resources. We will first work with the stripplot function and display a strip plot of the gross from each type of show. This means that we will be working with the categorical variable "Type" and the numeric/continous variable "Gross." The parameters that we have to pass in to the seaborn.stripplot() function are our x axis, our y axis and our data.<jupyter_code>sns.stripplot(x='Type', y = 'Gross', data = df)
plt.show()<jupyter_output><empty_output><jupyter_text>AnalysisAs you can see, the distributions of the gross for each type of show is shown above with the type of show as the x-axis and the gross as the y-axis.By comparing the distributions for each type, we can see how the distribution of the gross changes based on which type of show we are looking at. For the plays in the dataset, a large portion had made around 50,000 to 4,000,000 dollars per week. Meanwhile the distribution for a musical is far more spread out, with the majority of shows making 3,000,000 to 8,500,000 dollars per week.The special category has only a handful of points compared to the other categories and the distribution is also a lot less unified since there is no clustering of points.Swarm plots are very similar in analysis to strip plots since they depict the same types of variables with points.  Exercise 1:Draw a comparison of the attendance among the different types of Broadway shows i.e. Play, Musical, Special using a strip plot. And this time, change the radius of the dots to be 3 points wide. Be sure to look at the resources above for hints or syntax help if you need them.<jupyter_code>sns.stripplot(data=df, x='Type', y='Attendance', size=3)
plt.show()<jupyter_output><empty_output><jupyter_text>Now that we have learned about the strip plot, we can move on to the next distribution plot: the swarm plot.The parameters that we need are the exact same as for the stripplot() function. * X axis * Y axis* DataAre the parameters needed for seaborn.swarmplot(). Now try it yourself with the "Gross" and "Type" column.   Exercise 2:<jupyter_code>sns.swarmplot(x='Type', y = 'Gross', data = df, size=3)
plt.show()<jupyter_output><empty_output><jupyter_text>Exercise 3:Now use a swarm plot for the same data in Exercise 1. What is the visual difference in the data? When would you use this instead of a strip plot?<jupyter_code>sns.swarmplot(data=df, x='Type', y='Attendance', size=1)
plt.show()<jupyter_output><empty_output><jupyter_text>In a swarm plot, the datapoints do not layer on eachother, but spread around each other. This is useful when you want to see generally how many datapoints there are for a certain y value, which can be difficult with a strip plot.   Exercise 4:What is a limitation of using a swarm plot? (Pay attention to the messages given when running your code for the swarm plot exercise) It can de difficult to get all of your datapoints to fit on the graph. You will likely need to make your datapoints smaller, like I did.   Other Ways of Plotting with Categorical Data Violin PlotsViolin Plots are very closely related to Swarm, Strip and Box plots. They are most useful for visualizing the distribution or kernel density of data. They are also named that way due to their attractiveness and resemblance to a violin. But keep in mind that the distribution estimation procedure is influenced by the sample size, and violins for relatively small samples might look misleadingly smooth. For example if we wanted to visualize how "Gross" is distributed across the diferent types of broadway events we would do this:<jupyter_code>sns.violinplot(x = 'Type', y = 'Gross', data = df )
plt.show()<jupyter_output><empty_output><jupyter_text>AnalysisSource: [How to interpret Violin Plots](https://towardsdatascience.com/violin-plots-explained-fb1d115e023d)One can see that the gross of the Musicals has a median at about 0.7E6 which is much higher than that of the Play or Special events. We can also notice the distribution of the Play data has multiple peaks and that the Special data has one peak at the median (almost normally distributed) sitting within the interquartile range.   Exercise 5:Now it's your turn to try to plot a violin plot. Try pick any categorical variable and numerical variable from the dataframe and visualize it.<jupyter_code>sns.violinplot(data=df, x='Type', y='Attendance')
plt.show()<jupyter_output><empty_output><jupyter_text>Box PlotsAnother method of visualizing categorical data is using box plots. They offer an alternative to violin or strip/swarm plots with the advantage that you are able to quantify the quartiles of the data distribution (although this can also be done with a violin plot). The function seaborn.boxplot() is used for this. Notice that it takes the same basic parameters as swarmplot() and stripplot()<jupyter_code>sns.boxplot(x = "Type", y = "Gross", data = df)
plt.show()<jupyter_output><empty_output><jupyter_text>AnalysisEach plot is formed with 5 main points: the 1st and 3rd quartile points (the outer lines of the box), the median (the middle line), and the minimum and maximum (the outer lines on the plot). The area taken up by the actual box in each plot represents the middle 50% of each type of categorical data, and the middle line is the median of that data. The Musical Type category is the only plot that contains an outlier, as shown by the point below the minimum on the plot. We can see that 50-75% of the Musicals in the data grossed more than the maximum grossing Play, and the minimum grossing Musical grossed more than the maximum grossing Special. This is strong evidence that plays grossed significantly higher than Plays and Specials.  Exercise 6Try pick any categorical variable and numerical variable from the dataframe and visualize it.<jupyter_code>sns.boxplot(data=df, x='Type', y='Attendance')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0651507139079852, 'fraction_numerical': 0.013643574828133264, 'mean_word_length': 5.200655737704918, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 7, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '70336', 'license': 'MIT', 'n_tokens_mistral': 2717, 'n_tokens_neox': 2534, 'n_words': 1313, 'path': 'VisualizationLessons/swarm_plots.ipynb', 'repo_name': 'copl68/367FirstAssignment', 'size': '9482'}