简体   繁体   中英

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.

    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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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