2

I know there's many questions about this (e.g. here), I just can't see what I'm doing wrong.

I have this data:

Prod T1 T2
A  0  4
B  0  6.7
C  0  8.8
D  0  6.8
E  0  6.75
F  0  7.8
G  0  33.5
H  0  21

I want the plot to have 8 lines (A-H), each line showing growth from time period 1 (T1) to time period 2 (T2).

I wrote this code:

# libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# Data
df=pd.DataFrame({'Product':['A','B','C', 'D','E','F','G','H'], 'Time Period 2': ['4','6.7','8.8','6.8','6.75','7.8','33.5','21.00']})

# multiple line plot
plt.plot('Product','Time Period 2',data=df,marker='o',color='orange',linewidth=2)
plt.legend(loc='upper left')

The output is each product (A-H) on the x axis and the T2 numbers on the Y axis:

But what I want is for each product to have it's own growth line from T1 to T2 (e.g. like this example from the internet):

Can someone show me how to change this so the X axis just has two values 'time 1' and time 2', then there are multiple lines (A-H), showing growth from T1 to T2 (so e.g. for A, the T1 y value is 0 and the T2 y value is 4).

2 Answers 2

3

I think you just have to add .plots and they will be added on the same graph, for example you can do this:

# libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# Data
df=pd.DataFrame({'Product':['A','B','C', 'D','E','F','G','H'], 'Time Period 2': 
['4','6.7','8.8','6.8','6.75','7.8','33.5','21.00']})

df2=pd.DataFrame({'Product':['A','B','C', 'D','E','F','G','H'], 'Time Period 3': 
['12','13','14','15','16','17','18','19']})

# multiple line plot
plt.plot('Product','Time Period 2',data=df,marker='o',color='orange',linewidth=2)
plt.plot('Product','Time Period 3',data=df2,marker='o',color='orange',linewidth=2)
plt.legend(loc='upper left')
Sign up to request clarification or add additional context in comments.

Comments

1

I don't know if that can help you or if i understood the question, but i did that :

import matplotlib.pyplot as plt

x = ['A','B','C','D','E','F','G','H']
y = [0,0,0,0,0,0,0,0]
y2 = [4,6.7,8.8,6.8,6.75,7.8,33.5,21]

fig= plt.figure(figsize=(10,6))
plt.title("title")
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.plot(x,y)
plt.plot(x,y2)
plt.show()

Here is the result I had :

Comments

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.