首页 > 解决方案 > 在 matplotlib 图中未绘制几个数据集

问题描述

我想绘制一些我拥有的数据example.csv

SET,A,B,C,D,E,F,G,H
1,0.994462,1.00884,1.01166,1.03135,1.01513,1.0271,1.00742,1
2,1.97788,2.03412,2.00551,2.01214,1.96398,2.01321,2.00843,2
3,2.9503,3.03417,2.99962,3.02731,2.971,3.04173,2.97542,3
4,3.91176,4.05089,3.96083,4.03704,3.93824,4.11579,3.98413,4
5,4.86232,5.09019,4.96873,5.05585,4.93624,5.10578,4.99104,5
6,5.80201,6.16363,5.95535,6.1111,5.90442,6.11832,5.98018,6
7,6.73089,7.24463,6.90988,7.11387,6.85134,7.2216,6.99409,7
8,7.64898,8.29101,7.91987,8.19758,7.81935,8.35034,7.86887,8
9,8.55633,9.35712,8.8722,9.30687,8.74356,9.4471,8.92129,9
10,9.45299,10.45821,9.87314,10.35726,9.67969,10.46708,9.91542,10
15,13.7772,16.07988,14.69756,15.6466,14.44578,16.12539,14.80063,15
20,17.8397,21.93155,19.42845,21.28007,18.90318,22.15121,19.56829,20
25,21.6446,28.51192,23.97042,26.98871,23.12925,28.84552,24.33234,25
30,25.1951,34.80059,28.57095,33.30161,27.1875,35.9472,29.19356,30
35,28.4936,42.68168,32.59935,40.09501,31.54878,43.76342,33.6869,35
40,31.5409,,,,,,,
45,34.3362,,,,,,,

我已经编写了下面的代码,它几乎可以生成我想要的图形;但是,图中没有显示线“G”和“H”。我故意不想在人物的传说中出现 H;这可能是我所犯错误的一个因素吗?我不确定我做错了什么,希望能得到一些帮助。

import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.ticker import AutoMinorLocator
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

data = pd.read_csv('example.csv', sep=',')


class Test:
    def __init__(self, x, y, marker, linestyle, color):
        self.abcissa = x
        self.ordinate = y
        self.marker = marker
        self.linestyle = linestyle
        self.color = color


l_set_p = data["SET"].tolist()
l_A = data["A"].tolist()
l_B = data["B"].tolist()
l_C = data["C"].tolist()
l_D = data["D"].tolist()
l_E = data["E"].tolist()
l_F = data["F"].tolist()
l_G = data["G"].tolist()
l_H = data["H"].tolist()

A = Test(l_set_p, l_A, "^", "--", "#CC2936")
B = Test(l_set_p, l_B, "o", "-", "#CC2936")
C = Test(l_set_p, l_C, "^", "--", "#08415C")
D = Test(l_set_p, l_D, "o", "-", "#08415C")
E = Test(l_set_p, l_E, "^", "--", "#e3b505")
F = Test(l_set_p, l_F, "o", "-", "#e3b505")
G = Test(l_G, l_set_p, "None", "None", "#008148")
H = Test(l_set_p, l_H, "None", "None", "grey")

tests = [A, B, C, D, E, F, G, H]

ax = plt.axes()
for test in tests:
    ax.plot(
        test.abcissa,
        test.ordinate,
        marker=test.marker,
        linestyle=test.linestyle,
        color=test.color)

plt.legend(["A", "B", "C", "D", "E", "F", "G"],
           bbox_to_anchor=(0.5, -.25),
           ncol=3,
           loc=10)

axins0 = inset_axes(ax, width="30%", height="30%", loc=2)

for test in tests:
    axins0.plot(
        test.abcissa[:8],
        test.ordinate[:8],
        marker=test.marker,
        linestyle=test.linestyle,
        color=test.color)

axins0.tick_params(labelleft=False, left=False, direction='out')

axins1 = inset_axes(ax, width="30%", height="30%", loc=4)

for test in tests:
    axins1.plot(
        test.abcissa[13:],
        test.ordinate[13:],
        marker=test.marker,
        linestyle=test.linestyle,
        color=test.color)

axins1.xaxis.set_ticks_position("top")
axins1.tick_params(labelleft=False, left=False, direction='out')

plt.savefig('example.png', bbox_inches='tight', dpi=600)

在此处输入图像描述

标签: pythonmatplotlib

解决方案


推荐阅读