官术网_书友最值得收藏!

Adding horizontal and vertical lines

We will begin by importing our required libraries, as shown:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
# Set up figure size and DPI for screen demo
plt.rcParams['figure.figsize'] = (6,4)
plt.rcParams['figure.dpi'] = 150
  1. We will create the simple sine plot that we saw in Chapter 1, Heavy Customization, as follows:
# Adding a horizontal and vertical line
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))

We will get the following output:

  1. Now, to add an annotation, say, a line that splits the region between stuff above and below 0.5, add a horizontal line using axhline(0.5), as shown here. ax stands for the x axis and gives a value in the y co-ordinate for the horizontal line:
# Adding a horizontal and vertical line
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axhline(0.5)

We will get the following output:

  1. To color this horizontal line red, insert the following code:
# Adding a horizontal and vertical line
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axhline(0.5, color='r')

We will get the following output:

  1. To add a vertical line right at the first maximum, input pi/2 and color this red, along with a dashed line:
# Adding a horizontal and vertical line
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axhline(0.5, color='r')
plt.axvline(np.pi/2., color='r', linestyle='--')

Here we can see axv for the vertical line instead of axhline:

主站蜘蛛池模板: 韶山市| 葵青区| 溆浦县| 德兴市| 万山特区| 焉耆| 元阳县| 广西| 泾川县| 措美县| 镇宁| 丰城市| 丹阳市| 榆社县| 岢岚县| 屏山县| 新密市| 伽师县| 芦山县| 手机| 江川县| 武乡县| 城步| 巩义市| 兴隆县| 安丘市| 阿鲁科尔沁旗| 辉南县| 岚皋县| 资阳市| 通海县| 宁南县| 六盘水市| 泸州市| 鹿邑县| 常德市| 沐川县| 屯留县| 大城县| 抚顺市| 正蓝旗|