How do I plot weather data from two data sets on one bar graph using python?

Viewed 607

Python newbie here. I'm looking at some daily weather data for a couple of cities over the course of a year. Each city has its own csv file. I'm interested in comparing the count of daily average temperatures between two cities in a bar graph, so I can see (for example) how often the average temperature in Seattle was 75 degrees (or 30 or 100) compared to Phoenix.

I'd like a bar graph with side-by-side bars with temperature on the x-axis and count on the y-axis. I've been able to get a bar graph of each city separately with this data, but don't know how to get both cities on the same bar chart with with a different color for each city. Seems like it should be pretty simple, but my hours of search haven't gotten me a good answer yet.

Suggestions please, oh wise stackoverflow mentors?

Here's what I've got so far:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

df = pd.read_csv("KSEA.csv")
df2 = pd.read_csv("KPHX.csv")

df["actual_mean_temp"].value_counts(sort=False).plot(kind ="bar")
df2["actual_mean_temp"].value_counts(sort = False).plot(kind = 'bar')
1 Answers

You can concat DataFrames, assigning city as a column, and then use histplot in seaborn:

import seaborn as sns

z = pd.concat([
    df[['actual_mean_temp']].assign(city='KSEA'),
    df2[['actual_mean_temp']].assign(city='KPHX'),
])

ax = sns.histplot(data=z, x='actual_mean_temp', hue='city',
                  multiple='dodge', binwidth=1)

Output:

image

Related