1

My main goal is to use my CSV file and produce 2 bar charts one that shows the average temperature of the summer for every year (june-september) and another bar char that will show the average temperature for the yearly winter (december-march).

import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv('njtemp.csv')
df = pd.DataFrame(data)
X = list(df.iloc[:, 0])
Y = list(df.iloc[:, 13])
plt.bar(X, Y)
plt.title("NJ Annual Average Temp")
plt.xlabel("Years")
plt.ylabel("AVG ANNUAL TEMP")
plt.show()

a view of the csv file

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
swagmoney
  • 11
  • 2
  • 1
    Please do not link or embed external images of source code or data. Images make it difficult to efficiently assist you as they cannot be copied and offer poor usability as they cannot be searched. See: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/15497888) If you need assistance formatting a small sample of your DataFrame as a copyable piece of code for SO see [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/15497888). – Henry Ecker Mar 05 '22 at 01:15
  • I suggest reading the [tutorials first](https://pandas.pydata.org/pandas-docs/stable/user_guide/visualization.html#bar-plots) - you will find examples to solve common problems there. If you have a specific question regarding the implementation, please see the comment by Henry Ecker. – Mr. T Mar 05 '22 at 13:04

1 Answers1

1

How about this?

import pandas as pd

df = pd.read_csv('njtemp.csv',delimiter='\t', index_col=['Year'])

filter = ['Dec', 'Jan', 'Feb', 'Mar']

# calculate summer and winter acerage
df['avg_winter'] = df[filter].mean(axis=1)

filter.append("Annual")
df['avg_summer'] = df[df.columns.difference(filter)].mean(axis=1)


# plot all columns
ax = df[['avg_winter', 'avg_summer', 'Annual']].plot.bar()
ax.figure.savefig('test.png')

Output:

Your output might look like similar to this one. I used dummy data, as you didn't provide us with the raw data.

enter image description here

Fist, filter for winter months and calculate the average temperature above all selected columns, then calculate the average across the summer months. Finally use pandas in-built plotting functions to create a plot.

KarelZe
  • 1,466
  • 1
  • 11
  • 21