03_设置轴标签和范围、轴的标签(Labels on Axes)、定义轴的范围、使用linspace定义X值 (“linspace“ to Define X Values)
生活随笔
收集整理的這篇文章主要介紹了
03_设置轴标签和范围、轴的标签(Labels on Axes)、定义轴的范围、使用linspace定义X值 (“linspace“ to Define X Values)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
3.設(shè)置軸標(biāo)簽和范圍
3.1.軸的標(biāo)簽(Labels on Axes)
3.2.定義軸的范圍
3.3.使用linspace定義X值 (“l(fā)inspace” to Define X Values)
3.設(shè)置軸標(biāo)簽和范圍
3.1.軸的標(biāo)簽(Labels on Axes)
我們可以向軸添加標(biāo)簽來改善圖形的外觀。 這可以通過pyplot的ylabel和xlabel函數(shù)來完成。
import matplotlib.pyplot as plt days = list(range(1, 9)) print(days) ''' 輸出結(jié)果: [1, 2, 3, 4, 5, 6, 7, 8] ''' celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1] plt.plot(days, celsius_values) plt.xlabel('Day') plt.ylabel('Degrees Celsius') plt.show()可以在繪圖函數(shù)plot中指定任意數(shù)量的x,y,fmt組。 在以下示例中,我們使用兩個不同的y值列表:
import matplotlib.pyplot as plt days = list(range(1, 9)) print(days) ''' 輸出結(jié)果: [1, 2, 3, 4, 5, 6, 7, 8] ''' celsius_min = [19.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1] celsius_max = [24.8, 28.9, 31.3, 33.0, 34.9, 35.6, 38.4, 39.2] plt.xlabel('Day') plt.ylabel('Degrees Celsius') plt.plot(days, celsius_min, "oy", # o表示離散的days, celsius_max, "or") plt.show()再如:
import matplotlib.pyplot as plt days = list(range(1, 9)) print(days) ''' 輸出結(jié)果: [1, 2, 3, 4, 5, 6, 7, 8] ''' celsius_min = [19.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1] celsius_max = [24.8, 28.9, 31.3, 33.0, 34.9, 35.6, 38.4, 39.2] plt.xlabel('Day') plt.ylabel('Degrees Celsius') plt.plot(days, celsius_min,days, celsius_min, "oy",days, celsius_max,days, celsius_max, "or") plt.show()3.2.定義軸的范圍
我們還可以使用函數(shù)axis查看和定義軸的范圍。 如果不帶參數(shù)調(diào)用它,則返回當(dāng)前軸的limits:
import matplotlib.pyplot as plt days = list(range(1, 9)) print(days) ''' 輸出結(jié)果: [1, 2, 3, 4, 5, 6, 7, 8] ''' celsius_min = [19.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1] celsius_max = [24.8, 28.9, 31.3, 33.0, 34.9, 35.6, 38.4, 39.2] plt.xlabel('Day') plt.ylabel('Degrees Celsius') plt.plot(days, celsius_min,days, celsius_min, "oy",days, celsius_max,days, celsius_max, "or") print("The current limits for the axes are:") print(plt.axis()) print("We set axes to the following values:") xmin, xmax, ymin, ymax = 0, 10, 14, 45 print(xmin, xmax, ymin, ymax) plt.axis([xmin, xmax, ymin, ymax]) plt.show() """ 總的輸出結(jié)果: [1, 2, 3, 4, 5, 6, 7, 8] The current limits for the axes are: (0.6499999999999999, 8.35, 18.62, 40.18) We set axes to the following values: 0 10 14 45 """3.3.使用linspace定義X值 (“l(fā)inspace” to Define X Values)
我們在以下示例中使用Numpy函數(shù)linspace。 linspace可用于在指定間隔內(nèi)創(chuàng)建均勻分布的數(shù)字。
import numpy as np import matplotlib.pyplot as plt X = np.linspace(0, 2 * np.pi, 50, endpoint=True) F = np.sin(X) plt.plot(X,F) startx, endx = -0.1, 2*np.pi + 0.1 starty, endy = -1.1, 1.1 plt.axis([startx, endx, starty, endy]) plt.show() import numpy as np import matplotlib.pyplot as plt X = np.linspace(-2 * np.pi, 2 * np.pi, 50, endpoint=True) F1 = 3 * np.sin(X) F2 = np.sin(2*X) F3 = 0.3 * np.sin(X) startx, endx = -2 * np.pi - 0.1 , 2* np.pi + 0.1 starty, endy = -3.1, 3.1 plt.axis([startx, endx, starty, endy]) plt.plot(X, F1) plt.plot(X, F2) plt.plot(X, F3) plt.show()下一個例子中將在上圖的基礎(chǔ)上添加兩個具有離散點(diǎn)的圖:
import numpy as np import matplotlib.pyplot as plt X = np.linspace(-2 * np.pi, 2 * np.pi, 50, endpoint=True) F1 = 3 * np.sin(X) F2 = np.sin(2*X) F3 = 0.3 * np.sin(X) startx, endx = -2 * np.pi - 0.1 , 2* np.pi + 0.1 starty, endy = -3.1, 3.1 plt.axis([startx, endx, starty, endy]) plt.plot(X, F1) plt.plot(X, F2) plt.plot(X, F3) plt.plot(X, F1, 'ro') plt.plot(X, F2, 'bx') plt.show()總結(jié)
以上是生活随笔為你收集整理的03_设置轴标签和范围、轴的标签(Labels on Axes)、定义轴的范围、使用linspace定义X值 (“linspace“ to Define X Values)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 泉州市哪里有维修锂电池的?
- 下一篇: 04_05_06:设置线型风格(设置线型