0

I'm trying to draw the following chart using python. Can you help me out?

enter image description here

thanks

2 Answers 2

1

You can try this.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'realtime':[2,3,4,2,4],
                   'esttime':[1,1,3,1,4],
                   'time of 5 mins': ['09:15','09:20','09:25','09:30','09:35']})
df
   realtime  esttime time of 5 mins
0         2        1           9:15
1         3        1           9:20
2         4        3           9:25
3         2        1           9:30
4         4        4           9:35

Convert your time of 5 mins to valid datetime object using pd.to_datetime.

df['time of 5 mins']=pd.to_datetime(df['time of 5 mins'],format='%H:%M').dt.strftime('%H:%M')

Output:

Now, use time of 5 mins as X-Axis and Y-Axis for realtime and esttime and use matplotlib.pyplot.plot.annotate as 3-rd dimension.

index= ['A', 'B', 'C', 'D', 'E']

plt.plot(df['time of 5 mins'],df['esttime'],marker='o',alpha=0.8,color='#CD5C5C',lw=0.8)
plt.plot(df['time of 5 mins'],df['realtime'],marker='o',alpha=0.8,color='green',lw=0.8)

ax= plt.gca() #gca is get current axes

for i,txt in enumerate(index):
    ax.annotate(txt,(df['time of 5 mins'][i],df['realtime'][i]))
    ax.annotate(txt,(df['time of 5 mins'][i],df['esttime'][i]))
plt.show()

enter image description here

To make the plot more complete add legend, xlabel, ylabel, title, and stretch the X-Y Axis ranges a little so that it will be visually aesthetic. More details about matplotlib.pyplot here

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

Comments

1
import matplotlib.pyplot as plt
import numpy as np

y = [2, 3, 4, 2, 4]
y2 = [1, 1, 3, 1, 4]
a = ['9:15', '9:20', '9:25', '9:30', '9:35']
x = np.arange(5)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='Real Time')
ax.plot(x, y2, label='Estimated Time')
plt.xticks(x, labels=a)
plt.xlabel('Time')
chartBox = ax.get_position()
ax.set_position([chartBox.x0, chartBox.y0, chartBox.width*0.6, chartBox.height])
ax.legend(loc='upper center', bbox_to_anchor=(1.45, 0.8), shadow=True, ncol=1)
plt.show()

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.