matplotlib 如何在基本图上添加特定值的垂直散点图?

ntjbwcob  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(125)

我试图重现下面的曲线from this paper

该图显示了5次运行的平均准确度,垂直值显示了最小和最大准确度。
如何将这些具有特定值的垂直散点相加?
我的当前代码:

def plot_losses(losses: Dict[float, Dict[float, List[float]]]) -> None:
    """
    Plot the evolution of the loss regarding the sparsity level and iteration step

    Args:
        losses (Dict[float, Dict[float, List[float]]]): Dict containing the losses regarding the sparsity level and iteration step
    """

    plt.clf()

    plt.figure(figsize=(20, 10))
    plt.tight_layout()

    sparsity_levels = [round(sparsity_level, 2) for sparsity_level in losses.keys()]

    for sparsity_level, key in zip(sparsity_levels, losses.keys()):
        plt.plot(list(losses[key].keys()), list(losses[key].values()), '+--', label=f"{100 - sparsity_level:.2f}%")

    plt.show()
tp5buhyn

tp5buhyn1#

首选plt.errorbar(而不是plot_lossesfor 循环中的plt.plot),并使用参数yerr添加具有最小值和最大值的竖线。
以下是一个示例:

import numpy as np 
import matplotlib.pyplot as plt 

# Generate  data
x = np.arange(10) + 1
y1 = x/20
y2 = x/25

# Generate data for pseudo-errorbars
y1_err = np.array([y1[0::2]/20, y1[1::2]/7]).reshape(1, 10)
y2_err = np.array([y2[0::2]/30, y1[1::2]/13]).reshape(1, 10)

# Plot data
plt.errorbar(x, y1, yerr=y1_err, label="100", capsize=3, capthick=3.5)
plt.errorbar(x, y2, yerr=y2_err, label="51.3", capsize=3, capthick=3.5)

plt.legend(bbox_to_anchor=(0.95, 1.1), ncol=3)
plt.show()

这给出:

相关问题