Matplotlib:列表中的通道占用绘图

2024-06-09 19:59:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用Matplotlib创建频道占用图。让我解释一下我想做什么。你知道吗

例如,我有一个通信信道(例如电话线),由两个不同的方占用,如下表所示:

occupation_start_times = [[1,2,3],[1.5,2.5,3.5]]
occupation_durations = [[0.3,0.2,0.2],[0.4,0.2,0.1]]

所以基本上第一方占据了1到1.3,2到2.2,3到3.2的通道,第二方占据了1.5到1.9的通道,以此类推。你知道吗

对于每一个政党,我希望有一个不同的水平对齐的区域,每个频道占用的时期都用一个横条(或者更确切地说是一个矩形)来表示。我需要为每一方制定这样一个明确的计划,因为多方可能同时占领这个频道。你知道吗

我已经考虑过this,但问题是每个参与方只有一个单杠,而不是多个单杠被该参与方未占用频道的时间打断。你知道吗


Tags: 区域matplotlib水平频道starttimes信道occupation
1条回答
网友
1楼 · 发布于 2024-06-09 19:59:09

嘿,我可以通过@ImportanceOfBeingErnest链接解决我的问题!你知道吗

Picture

myplot类的一部分。。。只需删除所有selfimport matplotlib.pyplot as plt并根据您的需要进行定制,瞧!你知道吗

def broken_barh(self):
    plot_data = []
    for index,item in enumerate(self.data["occupation_starting"]):
        plot_data.append(list(zip(self.data["occupation_starting"][index], self.data["occupation_durations"][index])))

    print("plot_data:")
    #print(plot_data)
    data_len = len(plot_data)
    print(data_len)

    self.ax.set_ylim(5, 5*data_len+15)
    self.ax.set_xlim(0, self.timer*self.repetitions)
    self.ax.xaxis.grid(self.grid)
    self.ax.yaxis.grid(self.grid)

    # FIXME: Now ACKS are absolutely required, or data_len/2 doesn't make sense!
    self.ax.set_yticks([x*10+15 for x in range(int(data_len/2))])
    #self.ax.set_yticklabels(["measurement "+str(self.measurement[index]) for index in range(int(data_len/2))])

    self.setLabels(
        xlabel="time[s]",
        title=self.title
    )

    blue_patch  = mpatches.Patch(color='blue', label="Data")
    red_patch   = mpatches.Patch(color='red', label='Acks')

    if self.legend_coordinates[2] != "best":
        self.ax.legend( handles=[red_patch, blue_patch],
                        fancybox=True,
                        loc=self.legend_coordinates[2],
                        bbox_to_anchor=(self.legend_coordinates[0],
                                        self.legend_coordinates[1]))
    else:
        self.ax.legend( handles=[red_patch, blue_patch],
                        fancybox=True,
                        loc="best")

    for index,item in enumerate(plot_data):
        print("Added index "+str(index)+" to plot.")
        if index % 2 == 0:
            self.ax.broken_barh(item,((index+1)*5+5, 9), facecolors='blue')
        elif (index-1) % 2 == 0:
            self.ax.broken_barh(item,(index*5+5,9), facecolors='red')

相关问题 更多 >