-
-
Save ksv-muralidhar/e1ede2bb2af515ec025d3a0f8742b4dc to your computer and use it in GitHub Desktop.
stacked_plot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import matplotlib.pyplot as plt | |
import numpy as np | |
data = pd.read_csv('netflix_titles.csv') | |
data = data.loc[data['release_year'].isin([*range(2016, 2020)]), ['type', 'release_year']].copy() | |
data.dropna(inplace=True) | |
data['release_year'] = data['release_year'].astype('int') | |
data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cross_tab_prop.plot(kind='bar', | |
stacked=True, | |
colormap='tab10', | |
figsize=(10, 6)) | |
plt.legend(loc="lower left", ncol=2) | |
plt.xlabel("Release Year") | |
plt.ylabel("Proportion") | |
for n, x in enumerate([*cross_tab.index.values]): | |
for (proportion, count, y_loc) in zip(cross_tab_prop.loc[x], | |
cross_tab.loc[x], | |
cross_tab_prop.loc[x].cumsum()): | |
plt.text(x=n - 0.17, | |
y=(y_loc - proportion) + (proportion / 2), | |
s=f'{count}\n({np.round(proportion * 100, 1)}%)', | |
color="black", | |
fontsize=12, | |
fontweight="bold") | |
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cross_tab_prop.plot(kind='barh', | |
stacked=True, | |
colormap='tab10', | |
figsize=(10, 6)) | |
plt.legend(loc="lower left", ncol=2) | |
plt.ylabel("Release Year") | |
plt.xlabel("Proportion") | |
for n, x in enumerate([*cross_tab.index.values]): | |
for (proportion, count, y_loc) in zip(cross_tab_prop.loc[x], | |
cross_tab.loc[x], | |
cross_tab_prop.loc[x].cumsum()): | |
plt.text(x=(y_loc - proportion) + (proportion / 2), | |
y=n - 0.11, | |
s=f'{count}\n({np.round(proportion * 100, 1)}%)', | |
color="black", | |
fontsize=12, | |
fontweight="bold") | |
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cross_tab_prop = pd.crosstab(index=data['release_year'], | |
columns=data['type'], | |
normalize="index") | |
cross_tab_prop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cross_tab = pd.crosstab(index=data['release_year'], | |
columns=data['type']) | |
cross_tab |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cross_tab_prop.plot(kind='bar', | |
stacked=True, | |
colormap='tab10', | |
figsize=(10, 6)) | |
plt.legend(loc="upper left", ncol=2) | |
plt.xlabel("Release Year") | |
plt.ylabel("Proportion") | |
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cross_tab_prop.plot(kind='bar', | |
stacked=True, | |
colormap='tab10', | |
figsize=(10, 6)) | |
plt.legend(loc="upper left", ncol=2) | |
plt.xlabel("Release Year") | |
plt.ylabel("Proportion") | |
for n, x in enumerate([*cross_tab.index.values]): | |
for proportion in cross_tab_prop.loc[x]: | |
plt.text(x=n, | |
y=proportion, | |
s=f'{np.round(proportion * 100, 1)}%', | |
color="black", | |
fontsize=12, | |
fontweight="bold") | |
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for n, x in enumerate([*cross_tab.index.values]): | |
print(f'n: {n}, x: {x}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for n, x in enumerate([*cross_tab.index.values]): | |
for proportion in cross_tab_prop.loc[x]: | |
print(f'n: {n}, x: {x}, proportion: {proportion}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cross_tab_prop.plot(kind='bar', | |
stacked=True, | |
colormap='tab10', | |
figsize=(10, 6)) | |
plt.legend(loc="lower left", ncol=2) | |
plt.xlabel("Release Year") | |
plt.ylabel("Proportion") | |
for n, x in enumerate([*cross_tab.index.values]): | |
for (proportion, y_loc) in zip(cross_tab_prop.loc[x], | |
cross_tab_prop.loc[x].cumsum()): | |
plt.text(x=n - 0.17, | |
y=y_loc, | |
s=f'{np.round(proportion * 100, 1)}%', | |
color="black", | |
fontsize=12, | |
fontweight="bold") | |
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cross_tab_prop.plot(kind='bar', | |
stacked=True, | |
colormap='tab10', | |
figsize=(10, 6)) | |
plt.legend(loc="lower left", ncol=2) | |
plt.xlabel("Release Year") | |
plt.ylabel("Proportion") | |
for n, x in enumerate([*cross_tab.index.values]): | |
for (proportion, y_loc) in zip(cross_tab_prop.loc[x], | |
cross_tab_prop.loc[x].cumsum()): | |
plt.text(x=n - 0.17, | |
y=(y_loc - proportion) + (proportion / 2), | |
s=f'{np.round(proportion * 100, 1)}%', | |
color="black", | |
fontsize=12, | |
fontweight="bold") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment