5

I am in the processing of learning Python (Basic question coming) and I have lots of data that comes out of some analyses as CSV files that look like the following:

Example data table

I am trying to recreate the plot below (currently I do things like this in Excel and with the volume of data coming out and the complexity of the visualizations, I want to make things more efficient.

Plot I am trying to recreate

I tried to use the concept of For Loops to plot a line for each different "SectionName" in that column but am clearly missing something.

#read CSV
df=pd.read_csv('/Users/F/Documents/Data/LinesLoopTest.csv')

#Make Variables from dataframe columns
Value = df['Value']
Position = df['Xposition']
Section = df['SectionName']

#Setup Figure
fig = plt.figure(figsize=(6,3))
ax1 = fig.add_subplot(1,1,1)
ax1.set_title('Sections')
ax1.set_xlabel("XPosition")
ax1.set_ylabel("Value")

#plot lines by SectionName
for name in ['A', 'B', 'C']:
    plt.plot(Position, Value)

plt.show()

I realize this is a simple question, I just have not found an explanation so far where I can actually understand the process to the point that I can recreate it and then build on it.

1
  • Please don't post images of code, data, or Tracebacks. Copy and paste it as text then format it as code (select it and type ctrl-k) ... Discourage screenshots of code and/or errors Commented Apr 11, 2020 at 16:31

2 Answers 2

3

You can use a groupby on the SectionName and then plot the groups

fig, ax = plt.subplots()

for section, group in df.groupby('SectionName'):
    group.plot(x='Xposition', y='Value', ax=ax, label=section)

enter image description here

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

1 Comment

Hi @sheldore, if I want to put each line in each separate plot, what should be the right way of doing it?
2

I came late, so I did a pretty much a "copy/pastable" version of Sheldore's answer (His answer should me be approved)

import pandas as pd
import matplotlib.pyplot as plt 

# intialise data of lists. 
data = {'SectionName':['A','A','A', 'B','B','B', 'C','C','C'], 
        'Xpos':[1, 2, 3, 1, 2, 3, 1, 2, 3], 
        'Val':[0.2, 0.4, 0.5, 0.4, 1.3, 0.2, 1.2, 1.9, 1.8]} 

# Create DataFrame 
df = pd.DataFrame(data) 

fig, ax = plt.subplots()

for section, group in df.groupby('SectionName'):
    group.plot(x='Xpos', y='Val', ax=ax, label=section)
plt.show()

1 Comment

@Sheldore and Ivan (SO only lets me address one person), this is exactly what I needed, thank you! I see what you're doing with the groupby, excellent! Out of interest (this may need to be a separate question) is there a way to add another line that is the average line of all the "SectionNames." I can always make this a new question. Either way, thanks for the help!

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.