From d9246c1b42190eaaef06e0322da110f73380d8be Mon Sep 17 00:00:00 2001 From: SaaiVenkat <46614118+SaaiVenkat@users.noreply.github.com> Date: Wed, 7 Feb 2024 10:06:01 -0600 Subject: [PATCH] docs(example): add matplotlib dodged bar chart (#392) --- .../matplotlib/dodged_bar/dodged_bar.html | 1232 +++++++++++++++++ examples/matplotlib/dodged_bar/dodged_bar.py | 72 + examples/matplotlib/dodged_bar/dodged_bar.svg | 1149 +++++++++++++++ .../dodged_bar/dodged_bar_maidr.json | 63 + .../dodged_bar/dodged_bar_raw_data.json | 32 + 5 files changed, 2548 insertions(+) create mode 100644 examples/matplotlib/dodged_bar/dodged_bar.html create mode 100644 examples/matplotlib/dodged_bar/dodged_bar.py create mode 100644 examples/matplotlib/dodged_bar/dodged_bar.svg create mode 100644 examples/matplotlib/dodged_bar/dodged_bar_maidr.json create mode 100644 examples/matplotlib/dodged_bar/dodged_bar_raw_data.json diff --git a/examples/matplotlib/dodged_bar/dodged_bar.html b/examples/matplotlib/dodged_bar/dodged_bar.html new file mode 100644 index 00000000..13c5093e --- /dev/null +++ b/examples/matplotlib/dodged_bar/dodged_bar.html @@ -0,0 +1,1232 @@ + + + + + maidr Example + + + + + + + + + +
+ + + + + + 2024-02-01T12:05:15.861343 + image/svg+xml + + + Matplotlib v3.7.4, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + \ No newline at end of file diff --git a/examples/matplotlib/dodged_bar/dodged_bar.py b/examples/matplotlib/dodged_bar/dodged_bar.py new file mode 100644 index 00000000..8a752784 --- /dev/null +++ b/examples/matplotlib/dodged_bar/dodged_bar.py @@ -0,0 +1,72 @@ +import json +import matplotlib.pyplot as plt +import seaborn as sns + +# Load the dataset +titanic = sns.load_dataset("titanic") + +# Grouping the data by 'class' and 'survived', then counting the number of occurrences +grouped = titanic.groupby(['class', 'survived']).size().reset_index(name='count') + +# Sorting the DataFrame to ensure the correct order +grouped.sort_values(by=['survived', 'class'], ascending=[True, True], inplace=True) + +# Plotting the dodged bar chart +sns.barplot(x='class', y='count', hue='survived', data=grouped) + +# Adding labels and title +plt.title('Passenger Count by Class and Survival Status on the Titanic') +plt.xlabel('Class') +plt.ylabel('Number of Passengers') + +# Save the plot as an SVG file +plt.savefig('dodged_bar.svg', format='svg') + +# Generating the 'maidr' structure +maidr = { + "type": "dodged_bar", + "id": "dodged_bar", + "selector": "TODO: TYPE YOUR DODGED SELECTOR HERE", + "labels": { + "title": "Passenger Count by Class and Survival Status on the Titanic", + "x": "Class", + "y": "Number of Passengers", + "fill": "Survival Status" + }, + "axes": { + "x": { + "label": "Class", + "level": grouped['class'].unique().tolist(), + }, + "y": { + "label": "Number of Passengers", + }, + "fill": { + "label": "Survival Status", + "level": grouped['survived'].map({0: "Not Survived", 1: "Survived"}).unique()[::-1].tolist(), + }, + }, + "data": [] +} + +# Populate the data structure +for _, row in grouped.iterrows(): + maidr["data"].append({ + "x": row['class'].capitalize(), + "fill": "Survived" if row['survived'] else "Not Survived", + "y": row['count'] + }) + +# Writing the raw data to a JSON file +with open("dodged_bar_raw_data.json", 'w') as json_file: + json.dump(maidr["data"], json_file, indent=4) + +# Writing the dictionary to a JSON file +with open("dodged_bar_maidr.json", 'w') as json_file: + json.dump(maidr, json_file, indent=4) + +# Show the plot +plt.show() + +# Close the plot +plt.close() diff --git a/examples/matplotlib/dodged_bar/dodged_bar.svg b/examples/matplotlib/dodged_bar/dodged_bar.svg new file mode 100644 index 00000000..ebbf6ded --- /dev/null +++ b/examples/matplotlib/dodged_bar/dodged_bar.svg @@ -0,0 +1,1149 @@ + + + + + + + + 2024-02-01T12:05:15.861343 + image/svg+xml + + + Matplotlib v3.7.4, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/matplotlib/dodged_bar/dodged_bar_maidr.json b/examples/matplotlib/dodged_bar/dodged_bar_maidr.json new file mode 100644 index 00000000..a310abbc --- /dev/null +++ b/examples/matplotlib/dodged_bar/dodged_bar_maidr.json @@ -0,0 +1,63 @@ +{ + "type": "dodged_bar", + "id": "dodged_bar", + "selector": "TODO: TYPE YOUR DODGED SELECTOR HERE", + "labels": { + "title": "Passenger Count by Class and Survival Status on the Titanic", + "x": "Class", + "y": "Number of Passengers", + "fill": "Survival Status" + }, + "axes": { + "x": { + "label": "Class", + "level": [ + "First", + "Second", + "Third" + ] + }, + "y": { + "label": "Number of Passengers" + }, + "fill": { + "label": "Survival Status", + "level": [ + "Survived", + "Not Survived" + ] + } + }, + "data": [ + { + "x": "First", + "fill": "Not Survived", + "y": 80 + }, + { + "x": "Second", + "fill": "Not Survived", + "y": 97 + }, + { + "x": "Third", + "fill": "Not Survived", + "y": 372 + }, + { + "x": "First", + "fill": "Survived", + "y": 136 + }, + { + "x": "Second", + "fill": "Survived", + "y": 87 + }, + { + "x": "Third", + "fill": "Survived", + "y": 119 + } + ] +} \ No newline at end of file diff --git a/examples/matplotlib/dodged_bar/dodged_bar_raw_data.json b/examples/matplotlib/dodged_bar/dodged_bar_raw_data.json new file mode 100644 index 00000000..4d959f33 --- /dev/null +++ b/examples/matplotlib/dodged_bar/dodged_bar_raw_data.json @@ -0,0 +1,32 @@ +[ + { + "x": "First", + "fill": "Not Survived", + "y": 80 + }, + { + "x": "Second", + "fill": "Not Survived", + "y": 97 + }, + { + "x": "Third", + "fill": "Not Survived", + "y": 372 + }, + { + "x": "First", + "fill": "Survived", + "y": 136 + }, + { + "x": "Second", + "fill": "Survived", + "y": 87 + }, + { + "x": "Third", + "fill": "Survived", + "y": 119 + } +] \ No newline at end of file