matplotlib 如何在每条线上绘制不同的标记符号?

iecba09b  于 2023-04-12  发布在  其他
关注(0)|答案(1)|浏览(164)

如何在以下代码中的每一行上绘制不同的标记符号:

import matplotlib.pyplot as plt
import pandas as pd
from pathlib import Path
import numpy as np


def run(results_directory, optimizer, objectivefunc, Iterations):
    plt.ioff()
    fileResultsData = pd.read_csv(results_directory + "/exp.csv")
    plt.figure(figsize=(10, 10))
    for j in range(0, len(objectivefunc)):
        objective_name = objectivefunc[j]
        startIteration = 0
        if "SSA" in optimizer:
            startIteration = 1
        allGenerations = [x + 1 for x in range(startIteration, Iterations)]
        for i in range(len(optimizer)):
            optimizer_name = optimizer[i]

            row = fileResultsData[
                (fileResultsData["Optimizer"] == optimizer_name)
                & (fileResultsData["objfname"] == objective_name)
            ]
            row = row.iloc[:, 3 + startIteration :]
            plt.plot(allGenerations, row.values.tolist()[0],linestyle = 'dashed',label=optimizer_name)

        plt.title("Curve")
        plt.xlabel("Iterations")
        plt.ylabel("Fitness")
        plt.yscale("log")
        plt.legend(loc="upper right", bbox_to_anchor=(1.2, 1.02))
        plt.grid()
        fig_name = results_directory + "/convergence0-" + objective_name + ".png"
        plt.savefig(fig_name, bbox_inches="tight")
        plt.clf()

p = Path(r"C:\Users\sa\Desktop\C2017")
p.mkdir(parents=True, exist_ok=True)
run(str(p), ["HybridSSALEO","MFO","CSO","SSA",
             "HHO","PSO","SCA","WOA","BAT"],["f1"], 2500)

运行函数的数据样本

Optimizer   objfname    ExecutionTime   Iter1   Iter2   Iter3   Iter4   Iter5   Iter6   Iter7   ... Iter2491    Iter2492    Iter2493    Iter2494    Iter2495    Iter2496    Iter2497    Iter2498    Iter2499    Iter2500
0   HybridSSALEO    f1  7.43    2.580000e+12    2.290000e+12    2.170000e+12    2.050000e+12    1.820000e+12    1.700000e+12    1.650000e+12    ... 2104.890630 2104.880800 2104.874638 2104.868994 2104.863237 2104.857936 2104.853139 2104.849307 2104.843981 2104.829836
1   HybridSSALEO    f2  9.46    5.955520e+10    2.445214e+08    6.004195e+06    5.798884e+06    2.734759e+06    2.633134e+06    1.713644e+06    ... 375.136389  375.017438  374.943893  374.869639  374.687064  374.602179  374.544733  374.447893  374.358422  374.317267
2   HybridSSALEO    f3  14.09   1.516994e+09    2.770653e+05    2.728020e+05    2.684405e+05    2.169289e+05    2.123749e+05    2.080267e+05    ... 440.787196  440.777427  440.772892  440.748491  440.711001  440.698250  440.683562  440.677350  440.676047  440.668172
3   HybridSSALEO    f4  12.08   2.826890e+03    2.549340e+03    2.452620e+03    1.579159e+03    1.558178e+03    1.477120e+03    1.458300e+03    ... 788.193094  788.193094  788.193094  788.193094  788.193094  788.193094  788.193094  788.193094  788.193094  788.193094
4   HybridSSALEO    f5  21.16   8.074877e+02    8.014014e+02    7.957303e+02    7.931306e+02    7.913393e+02    7.872639e+02    7.811320e+02    ... 679.420922  679.420593  679.420393  679.420133  679.419760  679.419323  679.419027  679.418699  679.418348  679.417955
5 rows × 2503 columns

我有9条不同颜色的虚线,我希望如果有人能给予我一些提示来改变线条的风格或者如何在每条线上画出不同的形状。

b09cbbtk

b09cbbtk1#

在代码中,应循环遍历每一行,并执行以下代码。

from matplotlib.markers import MarkerStyle
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 10)
y = np.sin(x)

fig, ax = plt.subplots()

for i in range(10):
    ax.plot(x, y + (i / 2), marker=MarkerStyle.filled_markers[i], label=f'label{i}')

ax.legend(loc="upper left")

MarkerStyle.filled_markers有16种标记样式。如果您只需要9种标记样式,也没关系。
如果要更改线条样式

{
 '-': '_draw_solid',
 '--': '_draw_dashed',
 '-.': '_draw_dash_dot',
 ':': '_draw_dotted',
 'None': '_draw_nothing',
 ' ': '_draw_nothing',
 '': '_draw_nothing'
}

# from 
from matplotlib.lines import Line2D
Line2D.lineStyles

例如:

for i in range(0, 4):
    ax.plot(x,
            y + (i / 2),
            linestyle=list(Line2D.lineStyles.keys())[i],
            marker=MarkerStyle.filled_markers[i],
            label="Line " + str(i))

参照线型
参考标记样式

相关问题