玩命加载中...
### 4.图像的标注 通常我们不能只有图像,还需要适当标题、图例、坐标轴名称来让所作的图更完整。 `plt.title(my_string)`将会把plot的标题改为my_string。示例如下: ```python my_title = 'I added a title.' plt.figure(figsize=(5, 3)) plt.plot(x1, y1, lw=4, color='#32bab5', alpha=0.2) plt.plot(x2, y2, lw=6, color='#32bab5', alpha=0.6) plt.plot(x2, x2, lw=8, color='#32bab5', alpha=1.0) plt.title(my_title) plt.show() ``` ![png](output_33_0.png) `plt.xlabel(x_axis_name)`可将x-轴的名称改为`x_axis_name`。 同样`plt.ylabel(y_axis_name)`可将y-轴的名称改为`y_axis_name`。 ```python my_title = 'I added names of axes.' plt.figure(figsize=(5, 3)) plt.plot(x1, y1, lw=3, color='#32bab5', alpha=0.2) plt.plot(x2, y2, lw=6, color='#32bab5', alpha=0.6) plt.plot(x2, x2, lw=10, color='#32bab5', alpha=1.0) plt.title(my_title) plt.xlabel('this is x-axis') plt.ylabel('this is y-axis') plt.show() ``` ![png](output_35_0.png) 在每个折线对应的plt.plot里设置`label=label_name`,并且在`plt.show()`之前插入`plt.legdeng()`,我们就可以显示每条线的图例。 ```python my_title = 'I added a legend.' plt.figure(figsize=(5, 3)) plt.plot(x1, y1, lw=3, color='#32bab5', alpha=0.2, label='square') plt.plot(x2, y2, lw=6, color='#32bab5', alpha=0.6, label='root') plt.plot(x2, x2, lw=10, color='#32bab5', alpha=1.0, label='identity') plt.title(my_title) plt.xlabel('this is x-axis') plt.ylabel('this is y-axis') plt.legend() plt.show() ``` ![png](output_37_0.png) 有时候,图例会遮挡住图的一部分,我们可以设置`plt.legend()`中的参数`loc`来移动图例的位置。 ```python my_title = 'I moved the legend.' plt.figure(figsize=(5, 3)) plt.plot(x1, y1, lw=3, color='#32bab5', alpha=0.2, label='square') plt.plot(x2, y2, lw=6, color='#32bab5', alpha=0.6, label='root') plt.plot(x2, x2, lw=10, color='#32bab5', alpha=1.0, label='identity') plt.title(my_title) plt.xlabel('this is x-axis') plt.ylabel('this is y-axis') plt.legend(loc=[1.1, 0.5]) plt.show() ``` ![png](output_39_0.png) <ul class="pagination"> <li><a href="index.php">第1页</a></li> <li><a href="2.php">第2页</a></li> <li><a href="3.php">第3页</a></li> <li class="active"><a href="#">第4页</a></li> <li><a href="5.php">第5页</a></li> <li><a href="6.php">第6页</a></li> </ul> <ul class="pager"> <li class="previous"><a href="3.php"><b>&larr; 返回前一页</b></a></li> <li class="next"><a href="5.php"><b>进入下一页 &rarr;</b></a></li> </ul>