Nagendra18/Matplotlib-Seaborn
0
1import streamlit as st2import pandas as pd3import matplotlib.pyplot as plt4import seaborn as sns5 6st.title(":blue[Bar Plot]")7st.markdown("""8<svg height="10" width="100%">9 <path d="M0 0 Q50 10 100 0" stroke="pink" stroke-width="3" fill="transparent" />10</svg>11""", unsafe_allow_html=True)12 13 14st.markdown("""15A **bar plot** is a type of chart that represents categorical data with rectangular bars. The length of each bar corresponds to the value it represents. Bar plots can be used for univariate, bivariate, and multivariate analysis.16 17- **Univariate Analysis**: Bar plots visualize the distribution of a single categorical variable.18- **Bivariate Analysis**: They compare two variables, typically one categorical and one numerical.19- **Multivariate Analysis**: Bar plots can include multiple categorical variables or a numerical variable with different categories.20""")21st.markdown("""22- **Univariate Bar Plot**: Used to visualize the frequency or count of a single categorical variable.23- **Bivariate Bar Plot**: Helps to compare the relationship between a categorical and a numerical variable.24- **Multivariate Bar Plot**: Allows analysis of multiple variables by using the `hue` parameter to introduce a second categorical dimension.25""")26 27@st.cache_data28def load_data():29 url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"30 columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']31 return pd.read_csv(url, header=None, names=columns)32 33iris_data = load_data()34 35 36st.subheader("Univariate Analysis: Bar Plot using Matplotlib")37st.markdown("""38In univariate analysis, a bar plot is used to represent the frequency distribution of a single categorical variable, such as the count of each species in the Iris dataset.39""")40 41 42species_count = iris_data['species'].value_counts()43 44st.write("""45```python46plt.figure(figsize=(8, 6))47plt.bar(species_count.index, species_count.values, color='skyblue')48plt.title("Univariate Bar Plot - Species Count")49plt.xlabel("Species")50plt.ylabel("Count")51st.pyplot(plt)52```53""")54plt.figure(figsize=(8, 6))55plt.bar(species_count.index, species_count.values, color='skyblue')56plt.title("Univariate Bar Plot - Species Count")57plt.xlabel("Species")58plt.ylabel("Count")59st.pyplot(plt)60 61 62st.subheader("Bivariate Analysis: Bar Plot using Seaborn")63st.markdown("""64In bivariate analysis, a bar plot helps to compare a numerical variable against a categorical variable. For example, comparing the average `sepal_length` for each species.65""")66st.write("""67```python68plt.figure(figsize=(10, 6))69sns.barplot(x='species', y='sepal_length', data=iris_data, palette="muted", ci=None)70plt.title("Bivariate Bar Plot - Average Sepal Length by Species")71plt.xlabel("Species")72plt.ylabel("Average Sepal Length")73st.pyplot(plt)74```75""")76 77plt.figure(figsize=(10, 6))78sns.barplot(x='species', y='sepal_length', data=iris_data, palette="muted", ci=None)79plt.title("Bivariate Bar Plot - Average Sepal Length by Species")80plt.xlabel("Species")81plt.ylabel("Average Sepal Length")82st.pyplot(plt)83 84 85st.subheader("Multivariate Analysis: Bar Plot using Seaborn")86st.markdown("""87In multivariate analysis, we can use the `hue` parameter in Seaborn to add another categorical variable for further analysis. For example, we could visualize how the `sepal_width` varies across species.88""")89 90st.write("""91```python92plt.figure(figsize=(10, 6))93sns.barplot(x='species', y='sepal_length', data=iris_data, palette="muted", ci=None)94plt.title("Bivariate Bar Plot - Average Sepal Length by Species")95plt.xlabel("Species")96plt.ylabel("Average Sepal Length")97st.pyplot(plt)98```99""")100plt.figure(figsize=(10, 6))101sns.barplot(x='species', y='sepal_width', data=iris_data, hue='species', palette="coolwarm", ci=None)102plt.title("Multivariate Bar Plot - Sepal Width by Species")103plt.xlabel("Species")104plt.ylabel("Sepal Width")105st.pyplot(plt)106 107 108 109 110st.title(":blue[Count Plot Analysis]")111 112 113st.markdown("""114A **count plot** is used to show the count of observations in each categorical bin using bars. It is similar to a bar plot but specifically shows the count of data points for each category. Count plots can be used for univariate, bivariate, and multivariate analysis.115 116- **Univariate Analysis**: Displays the count of a single categorical variable.117- **Bivariate Analysis**: Compares the count of two variables (usually one categorical and one numerical).118- **Multivariate Analysis**: Displays counts for multiple categories or includes the `hue` parameter to distinguish between categories.119 120### Why Use Count Plots?121Count plots are extremely useful when working with categorical data and provide a clear visualization of the frequency of each category.122""")123 124st.subheader("Explanation of Count Plot")125st.markdown("""126- **Univariate Count Plot**: Displays the count of a single categorical variable. Here, we showed the count of each species in the Iris dataset.127- **Bivariate Count Plot**: Compares the count of observations across a categorical variable (e.g., species) for a second categorical feature.128- **Multivariate Count Plot**: Uses `hue` to include another categorical variable for comparison. This adds another layer of depth to the analysis.129""")130 131 132st.markdown("""133**Count plots** are essential for visualizing categorical data. They provide a quick view of the count or frequency of observations for different categories and can be extended to include multivariate analysis using the `hue` parameter in Seaborn.134 135- **Univariate Count Plot**: Used to analyze the frequency of a single categorical variable.136- **Bivariate Count Plot**: Compares one categorical variable against another, typically showing how they relate.137- **Multivariate Count Plot**: Adds additional categorical dimensions using the `hue` parameter to provide further insights.138""")139 140 141@st.cache_data142def load_data():143 url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"144 columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']145 return pd.read_csv(url, header=None, names=columns)146 147iris_data = load_data()148 149 150st.subheader("Univariate Analysis: Count Plot using Matplotlib")151st.markdown("""152In **univariate analysis**, a count plot can be used to display the frequency of a single categorical variable. Here, we use `species` in the Iris dataset.153""")154 155 156species_count = iris_data['species'].value_counts()157 158st.write("""159```python160plt.figure(figsize=(8, 6))161plt.bar(species_count.index, species_count.values, color='lightgreen')162plt.title("Univariate Count Plot - Species")163plt.xlabel("Species")164plt.ylabel("Count")165st.pyplot(plt)166```167""")168plt.figure(figsize=(8, 6))169plt.bar(species_count.index, species_count.values, color='lightgreen')170plt.title("Univariate Count Plot - Species")171plt.xlabel("Species")172plt.ylabel("Count")173st.pyplot(plt)174 175 176st.subheader("Bivariate Analysis: Count Plot using Seaborn")177st.markdown("""178In **bivariate analysis**, we visualize the relationship between two variables, typically one categorical and one numerical. For example, we can visualize the count of species across different categories of `sepal_width`.179""")180 181st.write("""182```python183plt.figure(figsize=(10, 6))184sns.countplot(x='species', data=iris_data, palette="pastel")185plt.title("Bivariate Count Plot - Species Distribution")186plt.xlabel("Species")187plt.ylabel("Count")188st.pyplot(plt)189```190""")191plt.figure(figsize=(10, 6))192sns.countplot(x='species', data=iris_data, palette="pastel")193plt.title("Bivariate Count Plot - Species Distribution")194plt.xlabel("Species")195plt.ylabel("Count")196st.pyplot(plt)197 198 199st.subheader("Multivariate Analysis: Count Plot using Seaborn with Hue")200st.markdown("""201In **multivariate analysis**, we can use the `hue` parameter in Seaborn to include a second categorical variable. This gives us insight into how the distribution of one variable is affected by another categorical variable.202""")203 204st.write("""205```python206plt.figure(figsize=(10, 6))207sns.countplot(x='species', data=iris_data, hue='species', palette="Set2")208plt.title("Multivariate Count Plot - Species with Hue")209plt.xlabel("Species")210plt.ylabel("Count")211st.pyplot(plt)212```213""")214plt.figure(figsize=(10, 6))215sns.countplot(x='species', data=iris_data, hue='species', palette="Set2")216plt.title("Multivariate Count Plot - Species with Hue")217plt.xlabel("Species")218plt.ylabel("Count")219st.pyplot(plt)220 221 222 223 224 