【问题标题】:Drawing 3 dimension using python matplotlib使用 python matplotlib 绘制 3 维
【发布时间】:2020-05-15 19:08:39
【问题描述】:

我正在尝试使用 python 绘制以下图表。 你能帮帮我吗?

谢谢

【问题讨论】:

    标签: python python-3.x matplotlib matplotlib-basemap


    【解决方案1】:

    你可以试试这个。

    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
    

    使用pd.to_datetime 将您的time of 5 mins 转换为有效的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 轴,将 realtimeesttime 用作 Y 轴,并使用 matplotlib.pyplot.plot.annotate 作为第三维。

    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()
    

    为了使情节更完整,请添加legendxlabelylabeltitle,并稍微拉伸X-Y Axis 范围,使其具有视觉美感。关于matplotlib.pyplot here的更多详情

    【讨论】:

      【解决方案2】:
      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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-19
        • 2019-02-07
        • 2022-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多