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

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:

主站蜘蛛池模板: 博野县| 黑水县| 保山市| 尖扎县| 威远县| 瓦房店市| 商洛市| 兴国县| 张家川| 江津市| 张家港市| 湾仔区| 临潭县| 松江区| 定西市| 明光市| 贵阳市| 尼木县| 卢龙县| 莒南县| 北安市| 抚松县| 汝州市| 雅安市| 吉首市| 苍溪县| 茶陵县| 黄石市| 赫章县| 灌阳县| 龙山县| 芦溪县| 九江县| 冀州市| 静宁县| 沅陵县| 大洼县| 渭南市| 广南县| 长岭县| 兴宁市|