使用python matplotlib绘制三维图。

问题描述 投票:0回答:1

我想用python画下面的图表,你能帮我吗?

enter image description here

谢谢

python matplotlib matplotlib-basemap
1个回答
4
投票

你可以试试这个。

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

转换你的 time of 5 mins 使之有效 datetime 使用 pd.to_datetime.

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

输出。

现在,使用 time of 5 mins 作为X轴和Y轴,为 realtimeesttime 并使用 matplotlib.pyplot.plot.annotate 作为第3维。

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

为了使情节更加完整,在图中添加 legend, xlabel, ylabel, title,并拉伸 X-Y Axis 范围一点,这样才会有视觉上的美感。更多关于 matplotlib.pyplot here


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()
© www.soinside.com 2019 - 2024. All rights reserved.