Skip to content

Instantly share code, notes, and snippets.

@billmetangmo
Created July 2, 2024 14:28
Show Gist options
  • Save billmetangmo/b0957c6329bb182313fd23756d1f5e3e to your computer and use it in GitHub Desktop.
Save billmetangmo/b0957c6329bb182313fd23756d1f5e3e to your computer and use it in GitHub Desktop.
Graph distribution of resources
import pandas as pd
import matplotlib.pyplot as plt
# Load the CSV file
file_path = '/mnt/data/resources(2).csv'
data = pd.read_csv(file_path)
# Calculate the number of resources per region
resources_per_region = data['Region'].value_counts().reset_index()
resources_per_region.columns = ['Region', 'Number of Resources']
# Add the Resources column with the link
resources_per_region['Resources'] = resources_per_region['Region'].apply(
lambda region: f'https://eu-west-1.console.aws.amazon.com/resource-groups/tag-editor/find-resources?region=eu-west-1#query=regions:!%28{region}%29,resourceTypes:!%28%27AWS::AllSupported%27%29,tagFilters:!%28%29,type:TAG_EDITOR_1_0'
)
# Display the dataframe to the user (this step is done using ace_tools, which might not be available in standard Python environments)
import ace_tools as tools; tools.display_dataframe_to_user(name="Resources per Region with Updated Links", dataframe=resources_per_region)
# Filter data for eu-west-1 and us-east-1
eu_west_1_data = data[data['Region'] == 'eu-west-1']
us_east_1_data = data[data['Region'] == 'us-east-1']
# Calculate the distribution of resource types for each region
eu_west_1_distribution = eu_west_1_data['Type'].value_counts()
us_east_1_distribution = us_east_1_data['Type'].value_counts()
# Plot horizontal bar chart for eu-west-1 with numbers next to bars
fig, ax = plt.subplots(figsize=(12, 16))
bars = ax.barh(eu_west_1_distribution.index, eu_west_1_distribution.values, color='skyblue')
ax.set_title('Distribution of Resource Types in eu-west-1')
ax.set_xlabel('Number of Resources')
ax.set_ylabel('Resource Type')
for bar in bars:
width = bar.get_width()
ax.text(width + 5, bar.get_y() + bar.get_height()/2, f'{int(width)}', va='center')
plt.tight_layout()
plt.show()
# Plot horizontal bar chart for us-east-1 with numbers next to bars
fig, ax = plt.subplots(figsize=(12, 16))
bars = ax.barh(us_east_1_distribution.index, us_east_1_distribution.values, color='lightgreen')
ax.set_title('Distribution of Resource Types in us-east-1')
ax.set_xlabel('Number of Resources')
ax.set_ylabel('Resource Type')
for bar in bars:
width = bar.get_width()
ax.text(width + 5, bar.get_y() + bar.get_height()/2, f'{int(width)}', va='center')
plt.tight_layout()
plt.show()
# Filter data for regions other than eu-west-1 and us-east-1
other_regions_data = data[~data['Region'].isin(['eu-west-1', 'us-east-1'])]
# Get unique regions from the filtered data
other_regions = other_regions_data['Region'].unique()
# Create a subplot for each region
fig, axes = plt.subplots(nrows=len(other_regions), ncols=1, figsize=(12, 5 * len(other_regions)))
# Plot for each region
for ax, region in zip(axes, other_regions):
region_data = other_regions_data[other_regions_data['Region'] == region]
region_distribution = region_data['Type'].value_counts()
bars = ax.barh(region_distribution.index, region_distribution.values, color='skyblue')
ax.set_title(f'Distribution of Resource Types in {region}')
ax.set_xlabel('Number of Resources')
ax.set_ylabel('Resource Type')
for bar in bars:
width = bar.get_width()
ax.text(width + 5, bar.get_y() + bar.get_height()/2, f'{int(width)}', va='center')
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment