【问题标题】:Round lines in MatplotlibMatplotlib 中的圆线
【发布时间】:2020-08-26 21:12:13
【问题描述】:

我想在 Matplotlib 中有一个带有曲线(平滑)线的图表。所以各个点应该用圆线连接。此外,我想在 y 线上没有值(只有描述)。代码如下:

from matplotlib import pyplot as plt
%matplotlib inline

load = [0.0, 0.1, 0.5, 0.7, 0.4, 0.55, 0.4, 0.3, 0.4, 0.5, 0.65, 0.75, 0.768, 0.75, 0.65, 0.5, 0.4, 0.3, 0.2, 0.15, 0.25, 0.4, 0.5, 0.4, 0.5]    
hours = list(range(25)) # [0, 1, 2, ... 22, 23, 24]
labels = [f'{h:02d}:00' for h in hours] # ["00:00", "01:00", ... "23:00", "24:00"]

fig = plt.figure(linewidth=1, figsize=(9, 5))
ax = plt.gca()


ax.plot(hours, load, color="goldenrod",drawstyle="default",  linewidth=3) # <- drawstyle argument.
ax.set_xlabel("Time of day", fontsize=14, labelpad=8)
ax.set_ylabel("Electrical power in W", fontsize=14, labelpad=8)
ax.set_xlim(0, 24)
ax.set_ylim(0, 1)    
plt.xticks(hours, labels=labels, rotation=90)

ax.tick_params(axis='both', which='major', labelsize=0)
# (Optional) ax.legend(loc='center left', bbox_to_anchor=(0.03, 1.15), fontsize = 14, ncol=3)
plt.tight_layout() # This must be called last, after all elements (plot and legend) are ready.
for item in [fig, ax]:
    item.patch.set_visible(False)
plt.savefig('CS_Curtailment_ElectricalLoad_NoFrame.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()

我认为属性“drawystyle”可能会改变。但是我不知道怎么做。 感谢您的每一条评论,并感谢您的帮助。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:
    from matplotlib import pyplot as plt
    from scipy.interpolate import interp1d
    import numpy as np
    load = [0.0, 0.1, 0.5, 0.7, 0.4, 0.55, 0.4, 0.3, 0.4, 0.5, 0.65, 0.75, 0.768, 0.75, 0.65, 0.5, 0.4, 0.3, 0.2, 0.15, 0.25, 0.4, 0.5, 0.4, 0.5]
    hours = list(range(25)) # [0, 1, 2, ... 22, 23, 24]
    labels = [f'{h:02d}:00' for h in hours] # ["00:00", "01:00", ... "23:00", "24:00"]
    
    
    f = interp1d(hours, load)
    f2 = interp1d(hours, load, kind='cubic')
    
    xnew = np.linspace(0, 24, num=500, endpoint=True)
    
    plt.xticks(np.arange(0, 25, step=1))  # Set label locations.
    plt.xticks(np.arange(25), labels)  # Set text labels.
    plt.xticks(np.arange(25), labels, rotation=90)
    
    plt.plot(hours, load, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--')
    plt.legend(['data', 'linear', 'cubic'], loc='best')
    plt.ylabel("Electrical power in W", fontsize=14, labelpad=8)
    plt.xlabel("Time of day", fontsize=14, labelpad=8)
    plt.show()
    
    
    plt.xticks(np.arange(0, 25, step=1))  # Set label locations.
    plt.xticks(np.arange(25), labels)  # Set text labels.
    plt.xticks(np.arange(25), labels, rotation=90)
    
    plt.plot(xnew, f2(xnew), color="green", linewidth=3)
    plt.legend(['cubic'], loc='best')
    plt.ylabel("Electrical power in W", fontsize=14, labelpad=8)
    plt.xlabel("Time of day", fontsize=14, labelpad=8)
    plt.show()
    

    Edit-1:x 轴标签旋转 90 度

    使用三次插值绘制图形:

    Edit-2:要隐藏到y轴变量,我们可以在plt.show()之前添加plt.tick_params(labelleft=False)。 绘制的图如下所示:

    Edit-3:绘制新负载。

    我们可以通过将这些行添加到代码中来绘制新的load 列表。

    load_2 = [0.0, 0.3, 0.2, 0.8, 0.1, 0.5, 0.2, 0.7, 0.4, 0.5, 0.34, 0.45, 0.768, 0.9, 0.25, 0.55, 0.2, 0.3, 0.2, 0.65, 0.25, 0.4, 0.2, 0.4, 0.5]
    f3 = interp1d(hours, load_2, kind='cubic', fill_value="extrapolate")
    xnew = np.linspace(0, 24, num=500, endpoint=True)
    
    plt.xticks(np.arange(0, 25, step=1))  # Set label locations.
    plt.xticks(np.arange(25), labels)  # Set text labels.
    plt.xticks(np.arange(25), labels, rotation=90)
    
    
    plt.plot(xnew, f3(xnew), color="red", linewidth=3)
    plt.legend(['cubic'], loc='best')
    plt.ylabel("Electrical power in W", fontsize=14, labelpad=8)
    plt.xlabel("Time of day", fontsize=14, labelpad=8)
    plt.tick_params(labelleft=False)
    plt.show()
    

    编辑-4:在一个图中绘制两条线:

    xnew = np.linspace(0, 24, num=500, endpoint=True)
    plt.xticks(np.arange(0, 25, step=1))  # Set label locations.
    plt.xticks(np.arange(25), labels)  # Set text labels.
    plt.xticks(np.arange(25), labels, rotation=90)
    
    
    plt.plot(xnew, f2(xnew), color="blue", linewidth=3)
    plt.plot(xnew, f3(xnew), color="red", linewidth=3)
    
    plt.legend(['load-1', 'load-2'], loc='best')
    plt.ylabel("Electrical power in W", fontsize=14, labelpad=8)
    plt.xlabel("Time of day", fontsize=14, labelpad=8)
    plt.tick_params(labelleft=False)
    plt.show()
    

    Edit-5:要填充两条线的曲线下区域,我们需要添加以下线:

    plt.fill_between(xnew, f2(xnew), color="blue", alpha=0.30, edgecolor=None)
    plt.fill_between(xnew, f3(xnew), color="red", alpha=0.30, edgecolor=None)
    

    绘制的图如下所示:

    【讨论】:

    【解决方案2】:

    我对曲线拟合没有任何经验,但我研究过基于this answer 对其进行自定义。 np.polyfit(x,y, deg)

    from matplotlib import pyplot as plt
    import numpy as np
    %matplotlib inline
    
    load = [0.0, 0.1, 0.5, 0.7, 0.4, 0.55, 0.4, 0.3, 0.4, 0.5, 0.65, 0.75, 0.768, 0.75, 0.65, 0.5, 0.4, 0.3, 0.2, 0.15, 0.25, 0.4, 0.5, 0.4, 0.5]    
    hours = list(range(25)) # [0, 1, 2, ... 22, 23, 24]
    labels = [f'{h:02d}:00' for h in hours] # ["00:00", "01:00", ... "23:00", "24:00"]
    
    poly = np.polyfit(hours, load, 8)
    poly_y = np.poly1d(poly)(hours)
    
    fig = plt.figure(linewidth=1, figsize=(9, 5))
    ax = plt.gca()
    
    # ax.plot(hours, load, color="goldenrod", ls='-', linewidth=3) # <- drawstyle argument.
    ax.plot(hours, poly_y, color="goldenrod", ls='-', linewidth=3)
    ax.set_xlabel("Time of day", fontsize=14, labelpad=8)
    ax.set_ylabel("Electrical power in W", fontsize=14, labelpad=8)
    ax.set_xlim(0, 24)
    ax.set_ylim(0, 1)    
    ax.set_xticks(hours)
    ax.set_xticklabels(labels, rotation=90)
    ax.set_yticks([])
    ax.tick_params(axis='both', which='major', labelsize=10)
    # (Optional) ax.legend(loc='center left', bbox_to_anchor=(0.03, 1.15), fontsize = 14, ncol=3)
    plt.tight_layout() # This must be called last, after all elements (plot and legend) are ready.
    # for item in [fig, ax]:
    #     item.patch.set_visible(False)
    plt.savefig('CS_Curtailment_ElectricalLoad_NoFrame.png', edgecolor='black', dpi=400, bbox_inches='tight')
    plt.show()
    

    【讨论】:

    • 感谢初学者。这不是我想要的。我希望点之间有平滑的连接,而不是它们之间的 picwise 线性函数。
    猜你喜欢
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多