I am trying to plot a graph from a csv using matplotlib and pandas with the following structure:
There are 36 total possible states and I would like each of them to be have one line. There are 100 Iterations. Iteration will start from 0 and is increased by 1 each time. Every state will have a record for every ith iteration. The lines are plotted with Iteration as the x axis and Utility as the y axis, visualized below.
I've made a dataframe out of the csv file but that is as far as I've gotten.
value_headers = ['State', 'Iteration', 'Utility']
value_df = pd.read_csv('example.csv', names = value_headers, index_col='State')
EDIT: Here is the dict of my dataframe
I've been wrecking by brain on this for the past hour to no avail. I don't even know how to describe this problem in a concise sentence for troubleshooting. Also, I wasn't able to find a question similar to mine on stackoverflow. Any advice will be greatly appreciated.
CodePudding user response:
If I read your DataFrame correctly, you need to figure out when a given state identifier appears in the first column of your csv and then use those values together to make a plot.
Based on your example with a single value of Iteration
, I suggest the following (assuming your DataFrame is called df
)
import matplotlib.pyplot as plt
import pandas as pd
fig, ax = plt.subplots()
df = df.iloc[1:,:]
df = df.reset_index()
# the state identifier is now a column with the header 'index'
for idx in df['index'].unique():
subset = df[df['index']==idx]
ax.plot(subset['Iteration'], subset['Utility'])
plt.show()