Hello @angusm112,
Welcome to the community!
It looks like something about your data isn’t quite right.
Have you made sure to sum the data before representing it?
Also, code helps go a long way. If you could please provide the example app for how this is demonstrating, then that will help us, help you.
Hi @angusm112 !
My guess is that you used a bar plot, you should try with a histogram, take a look here:
Or as suggested by @jinnyzor you should aggregate your data before render it with your bar plot.
That would be easier indeed with some code
Hi thank you for your help on this - yes I can show you the code:
its a matplotlib graph:
#PLOT 1
Sort the DataFrame by Crime Rate in descending order to highlight the highest crime rate state
usacrime_df_sorted = usacrime_df.sort_values(by=‘Crime Rate’, ascending=False)
Select the state with the highest crime rate
highest_crime_state = usacrime_df_sorted.iloc[0]
Plotting
plt.figure(figsize=(12, 8))
Plot bars for all states in grey
plt.bar(usacrime_df_sorted[‘States’], usacrime_df_sorted[‘Crime Rate’], color=‘grey’)
Highlight the highest crime rate state (Florida) in red
plt.bar(highest_crime_state[‘States’], highest_crime_state[‘Crime Rate’], color=‘red’, label=‘Florida’)
Annotate Florida’s crime rate on the graph
plt.text(highest_crime_state[‘States’], highest_crime_state[‘Crime Rate’], highest_crime_state[‘Crime Rate’],
ha=‘center’, va=‘bottom’)
plt.title(‘States with Crime Rates (1975-2015)’)
plt.xlabel(‘States’)
plt.ylabel(‘Crime Rate Count’)
plt.xticks(rotation=45, ha=‘right’)
plt.tight_layout() # Adjust layout to prevent clipping of labels
plt.legend() # Show legend
plt.show()
#Creating a new colomn called ‘Crime Rate Average’ to look at Average crime rates amoungst all States
Check the data types of each column
print(usacrime_df.dtypes)
If any of the columns are not numeric, you can convert them to numeric data type
usacrime_df[‘Crime Rate’] = pd.to_numeric(usacrime_df[‘Crime Rate’], errors=‘coerce’)
usacrime_df[‘Homicide Rate’] = pd.to_numeric(usacrime_df[‘Homicide Rate’], errors=‘coerce’)
usacrime_df[‘Rape Crime Rate’] = pd.to_numeric(usacrime_df[‘Rape Crime Rate’], errors=‘coerce’)
usacrime_df[‘Assault Rate’] = pd.to_numeric(usacrime_df[‘Assault Rate’], errors=‘coerce’)
usacrime_df[‘Robbery Rate’] = pd.to_numeric(usacrime_df[‘Robbery Rate’], errors=‘coerce’)
Now, calculate the mean
usacrime_df[‘Crime Rate Average’] = usacrime_df[[‘Crime Rate’, ‘Homicide Rate’, ‘Rape Crime Rate’, ‘Assault Rate’, ‘Robbery Rate’]].mean(axis=1)
Drop the decimal by converting to integer
usacrime_df[‘Crime Rate Average’] = usacrime_df[‘Crime Rate Average’].astype(int)
#LOOKING AT TOTAL CRIME RATE AVERAGE AMOUGNST ALL STATES
#PLOT 2
Sort the DataFrame by Crime Rate Average in descending order
usacrime_df_sorted = usacrime_df.sort_values(by=‘Crime Rate Average’, ascending=False)
Get the top state with the highest Crime Rate Average
top_state = usacrime_df_sorted.iloc[0]
Plot the bar chart
plt.figure(figsize=(10, 6))
plt.bar(usacrime_df_sorted[‘States’], usacrime_df_sorted[‘Crime Rate Average’], color=‘grey’) # Plot all bars in grey
plt.bar(top_state[‘States’], top_state[‘Crime Rate Average’], color=‘red’, label=f’{top_state[“States”]} Crime Rate Average’) # Highlight the top state in red
plt.xlabel(‘States’)
plt.ylabel(‘Crime Rate Average Count’)
plt.title(‘Crime Rate Average by State (1975-2015)’)
plt.xticks(rotation=45, ha=‘right’)
Annotate Florida’s crime rate
plt.annotate(f’{top_state[“Crime Rate Average”]}',
xy=(top_state[‘States’], top_state[‘Crime Rate Average’]),
xytext=(0, 3),
textcoords=‘offset points’,
ha=‘center’,
fontsize=10,
color=‘black’) # Adjust xytext to position the annotation above the bar
plt.legend()
plt.show()
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#PLOT 3
#Sort the DataFrame by Crime Rate Average in descending order
usacrime_df_sorted = usacrime_df.sort_values(by=‘Crime Rate Average’, ascending=False)
Get the top state with the highest Crime Rate Average
top_state = usacrime_df_sorted.iloc[0]
Get the state with the lowest Crime Rate Average
lowest_state = usacrime_df_sorted.iloc[-1]
Plot the bar chart
plt.figure(figsize=(10, 6))
Plot all bars in grey
plt.bar(usacrime_df_sorted[‘States’], usacrime_df_sorted[‘Crime Rate Average’], color=‘grey’)
Highlight the top state in red
plt.bar(top_state[‘States’], top_state[‘Crime Rate Average’], color=‘red’, label=f’{top_state[“States”]} Crime Rate Average’)
Highlight the lowest state in blue
plt.bar(lowest_state[‘States’], lowest_state[‘Crime Rate Average’], color=‘blue’, label=f’{lowest_state[“States”]} Crime Rate Average’)
Annotate top state’s crime rate
plt.annotate(f’{top_state[“Crime Rate Average”]}',
xy=(top_state[‘States’], top_state[‘Crime Rate Average’]),
xytext=(0, 3),
textcoords=‘offset points’,
ha=‘center’,
fontsize=10,
color=‘black’)
Annotate lowest state’s crime rate
plt.annotate(f’{lowest_state[“Crime Rate Average”]}',
xy=(lowest_state[‘States’], lowest_state[‘Crime Rate Average’]),
xytext=(0, 3),
textcoords=‘offset points’,
ha=‘center’,
fontsize=10,
color=‘black’)
Annotate Virginia’s crime rate above the bar
plt.annotate(f’{lowest_state[“Crime Rate Average”]}',
xy=(lowest_state[‘States’], lowest_state[‘Crime Rate Average’]),
xytext=(0, 3),
textcoords=‘offset points’,
ha=‘center’,
fontsize=10,
color=‘black’)
plt.xlabel(‘States’)
plt.ylabel(‘Crime Rate Average Count’)
plt.title(‘Crime Rate Average by State (1975-2015)’)
plt.xticks(rotation=45, ha=‘right’)
plt.legend()
plt.show()
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Filter the DataFrame to include only years between 2000 and 2015
usacrime_df_filtered = usacrime_df[(usacrime_df[‘Report Year’] >= 2000) & (usacrime_df[‘Report Year’] <= 2015)]
Sort the filtered DataFrame by Crime Rate in descending order to highlight the highest crime rate state
usacrime_df_sorted = usacrime_df_filtered.sort_values(by=‘Crime Rate’, ascending=False)
Select the state with the highest crime rate
highest_crime_state = usacrime_df_sorted.iloc[0]
Plotting
plt.figure(figsize=(12, 8))
Plot bars for all states in grey
plt.bar(usacrime_df_sorted[‘States’], usacrime_df_sorted[‘Crime Rate’], color=‘grey’)
Highlight the highest crime rate state (Florida) in red
plt.bar(highest_crime_state[‘States’], highest_crime_state[‘Crime Rate’], color=‘red’, label=‘Georiga’)
Annotate Florida’s crime rate on the graph
plt.text(highest_crime_state[‘States’], highest_crime_state[‘Crime Rate’], highest_crime_state[‘Crime Rate’],
ha=‘center’, va=‘bottom’)
plt.title(‘States with Crime Rates (2000-2015)’)
plt.xlabel(‘States’)
plt.ylabel(‘Crime Rate Count’)
plt.xticks(rotation=45, ha=‘right’) # Rotate x-axis labels for better readability
plt.tight_layout() # Adjust layout to prevent clipping of labels
plt.legend() # Show legend
plt.show()
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2222## Filter the DataFrame to include only years between 2000 and 2015
usacrime_df_filtered = usacrime_df[(usacrime_df[‘Report Year’] >= 2000) & (usacrime_df[‘Report Year’] <= 2015)]
Sort the filtered DataFrame by Crime Rate in descending order to highlight the highest crime rate state
usacrime_df_sorted = usacrime_df_filtered.sort_values(by=‘Crime Rate’, ascending=False)
Select the state with the highest crime rate
highest_crime_state = usacrime_df_sorted.iloc[0]
Select the state with the lowest crime rate
lowest_crime_state = usacrime_df_sorted.iloc[-1]
Plotting
plt.figure(figsize=(12, 8))
Plot bars for all states in grey
plt.bar(usacrime_df_sorted[‘States’], usacrime_df_sorted[‘Crime Rate’], color=‘grey’)
Highlight the highest crime rate state (Georgia) in red
plt.bar(highest_crime_state[‘States’], highest_crime_state[‘Crime Rate’], color=‘red’, label=‘Georgia’)
Annotate Georgia’s crime rate on the graph
plt.text(highest_crime_state[‘States’], highest_crime_state[‘Crime Rate’], highest_crime_state[‘Crime Rate’],
ha=‘center’, va=‘bottom’)
Plot bars for the lowest crime rate state (Virginia) in blue
plt.bar(lowest_crime_state[‘States’], lowest_crime_state[‘Crime Rate’], color=‘blue’, label=‘Virginia’)
Annotate Virginia’s crime rate on the graph
plt.text(lowest_crime_state[‘States’], lowest_crime_state[‘Crime Rate’], lowest_crime_state[‘Crime Rate’],
ha=‘center’, va=‘bottom’)
plt.title(‘States with Crime Rates (2000-2015)’)
plt.xlabel(‘States’)
plt.ylabel(‘Crime Rate Count’)
plt.xticks(rotation=45, ha=‘right’) # Rotate x-axis labels for better readability
plt.tight_layout() # Adjust layout to prevent clipping of labels
plt.legend() # Show legend
plt.show()