简体   繁体   中英

Label does not show in Matplotlib

I am plotting points to the graph. There are three lines. I want to give label for each line. I am doing like this for that are given below:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
x_axis = np.array([1, 3, 5, 7, 9])
y_axis1 = np.array([173.825, 139.386, 129.364, 123.281, 119.162])
e1 = np.array([1.895, 1.599, 1.551, 1.783, 1.272])

y_axis2 = np.array([161.168, 143.071, 135.891, 131.081, 126.970])
e2 = np.array([2.076, 1.700, 1.400, 1.295, 0.788])

y_axis3 = np.array([208.306, 198.804, 193.063, 189.554, 185.764])
e3 = np.array([1.314, 1.350, 1.046, 1.770, 1.135])

plt.axis([0, 11, 110, 210])
plt.errorbar(x_axis, y_axis1, e1, linestyle='solid', marker='.', ecolor='red', color='blue', label='Line1')
plt.errorbar(x_axis, y_axis2, e2, linestyle='solid', marker='.', ecolor='red', color='green', label='Line2')
plt.errorbar(x_axis, y_axis3, e3, linestyle='solid', marker='.', ecolor='red', color='yellow', label='Line3')
plt.xlabel('Data')
plt.ylabel('Average')

plt.show()

But the label does not show up. Here is the output:

在此输入图像描述

You should store your plot and then legend them :

plt1 = plt.errorbar(x_axis, y_axis1, e1, linestyle='solid', marker='.', ecolor='red', color='blue', label='Line1');
plt.legend(handles=[plt1]);
plt.show();

"plt.legend" will make the association between the line and their legend and then ou can show them.

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