简体   繁体   English

如何在matplotlib中添加标签以进行绘图

[英]How to add label to plot in matplotlib

This is the graph:这是图表: 在此处输入图片说明

I need to add label to graph lines.我需要为图形线添加标签。 Blue line represents left and red line represents right.蓝线代表左,红线代表右。 How do I do that?我怎么做? I used set_ylabel but that added label to left of graph as shown in it.我使用了 set_ylabel 但在图表左侧添加了标签,如图所示。

    self.fig = Figure(figsize=(6, 4), dpi=96)
    self.ax =  self.fig.add_subplot(111)
    self.graph = FigureCanvasTkAgg(self.fig, master=self.win)
    self.canvas = self.graph.get_tk_widget()
    a = self.df["index"].unique()
    line, = self.ax.plot(a,self.df.loc[self.df.foot == "right","total_force"].values)
    self.ax.set_ylabel("right")
    line2, = self.ax.plot(a,self.df.loc[self.df.foot == "left","total_force"].values)
    self.clean_button()
    self.clean_flush()
    self.canvas.place(x= 150, y = 5)

You have to specify something more when you call the plot() method: Instead of调用plot()方法时必须指定更多内容:而不是

line, = self.ax.plot(a,self.df.loc[self.df.foot == "right","total_force"].values)

Use

line, = self.ax.plot(a,self.df.loc[self.df.foot == "right","total_force"].values,
                     label = "right")

line2, = self.ax.plot(a,self.df.loc[self.df.foot == "left","total_force"].values
                      label = "left") 

And then show the legend using:然后使用以下方法显示图例:

self.ax.legend()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM