0

I'm struggling to plot my data on separate graphs (one graph, 1 line) and give them an appropriate "name" as a label.

import matplotlib.pyplot as plt 

d = pd.DataFrame({'Time_min': [1, 2, 3], 
                    'A1': [1000, 2000, 1000], 
                    'A12': [2000, 3000, 2000], 
                    'B12': [3000, 5000, 3000]})

template = pd.DataFrame({'well_id': ['A1', 'A12', 'B12'],
                         'name': ['Sample1', 'Sample2', 'Sample4']})

df1 = pd.merge(template, d.T, how='left', left_on='well_id', right_index=True)
df2 = df1.iloc[:,2:].T

print(d.head())
print(df1.head())

#for index, row in df2.iterrows():

plt.figure()
plt.plot(d.Time_min, df2, label = df1.iloc[:,1])
plt.legend()

plt.show()

In the end I want each of my graphs to look like this:

plt.plot(d.Time_min, d['A12'], label = df1.iloc[1,1])

But without specifying exact sample and it's label (name) visualized on the graph.

1 Answer 1

1

I'm not sure what output I'm expecting, but the following code can be used to achieve a graph that arranges 'Time_min' on the x-axis in ID units. Does this fit the intent of your question?

import matplotlib.pyplot as plt 

d = pd.DataFrame({'Time_min': [1, 2, 3], 
                    'A1': [1000, 2000, 1000], 
                    'A12': [2000, 3000, 2000], 
                    'B12': [3000, 5000, 3000]})

template = pd.DataFrame({'well_id': ['A1', 'A12', 'B12'],
                         'name': ['Sample1', 'Sample2', 'Sample4']})

df1 = pd.merge(template, d.T, how='left', left_on='well_id', right_index=True)
df1.columns = ['well_id', 'name', 'Time_min_1', 'Time_min_2','Time_min_3']

df1.plot(x='well_id', kind='bar')

plt.show()

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I added the code to show how I want my output to look like.
I've run the updated code and added a graph, is this graph what you're looking for? The expected graph is not clear to me.
Yes, that's exactly the type of graph I'm looking for. It's straightforward to make it for 1 graph, for every sample with the correct label, without specifying it manually. I just can't figure out how to use for? loop? functions to achieve that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.