- Mastering Matplotlib 2.x
- Benjamin Walter Keller
- 230字
- 2021-06-10 19:29:11
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
- 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:

- 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:
- 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:

- 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:

推薦閱讀
- Big Data Analytics with Hadoop 3
- 樂(lè)高機(jī)器人:WeDo編程與搭建指南
- 機(jī)器人智能運(yùn)動(dòng)規(guī)劃技術(shù)
- CorelDRAW X4中文版平面設(shè)計(jì)50例
- 21天學(xué)通Java
- 統(tǒng)計(jì)策略搜索強(qiáng)化學(xué)習(xí)方法及應(yīng)用
- 數(shù)據(jù)通信與計(jì)算機(jī)網(wǎng)絡(luò)
- 單片機(jī)技術(shù)一學(xué)就會(huì)
- 激光選區(qū)熔化3D打印技術(shù)
- Salesforce for Beginners
- Mastering GitLab 12
- 工業(yè)自動(dòng)化技術(shù)實(shí)訓(xùn)指導(dǎo)
- 工業(yè)機(jī)器人實(shí)操進(jìn)階手冊(cè)
- 步步驚“芯”
- Java組件設(shè)計(jì)