Python資料視覺化之matplotlib實踐chapter-01
阿新 • • 發佈:2018-12-16
""" Example 1.3.1: 函式plot() """ import matplotlib.pyplot as plt import numpy as np x = np.linspace(0.05,10,1000) # x = 0.05到10的等間距1000個點 y = np.cos(x) # y = cos(x) # ls=圖線風格, lw=圖線寬度, label=影象標籤 plt.plot(x,y,ls="-",lw="2",label="plot figure") # 繪製線圖 plt.legend() # 繪製圖例 plt.show() # 顯示影象
"""
Example 1.3.2:
函式scatter()
"""
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.random.rand(1000)
plt.scatter(x,y,label="scatter figure") #繪製散點圖
plt.legend()
plt.show()
""" Example 1.3.3: 函式xlim() """ import matplotlib.pyplot as plt import numpy as np x = np.linspace(0.05,10,1000) y = np.random.rand(1000) plt.scatter(x,y,label="scatter figure") plt.legend() plt.xlim(0.05,10) # 設定x軸範圍 plt.ylim(0,1) # 設定y軸範圍 plt.show()
"""
Example 1.3.4:
函式xlabel()
"""
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls='-',lw='2',label='plot figure')
plt.legend()
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()
""" Example 1.3.5: 函式grid() """ import matplotlib.pyplot as plt import numpy as np x = np.linspace(0.05,10,1000) y = np.sin(x) plt.plot(x,y,ls='-',lw='2',c='y',label='plot figure') plt.legend() plt.grid(linestyle=':',color='r') plt.show()
"""
Example 1.3.6:
函式axhline()
"""
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls='-',lw='2',c='y',label='plot figure')
plt.legend()
plt.axhline(y=0.0,c='b',ls='--',lw='2')
plt.axvline(x=0.0,c='b',ls='--',lw='2')
plt.show()
"""
Example 1.3.7:
函式axvspan()
"""
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls='-',lw='2',c='y',label='plot figure')
plt.legend()
plt.axvspan(xmin=4.0, xmax=6.0, facecolor='y',alpha=0.3)
plt.axhspan(ymin=0.0, ymax=0.5, facecolor='y',alpha=0.3)
plt.show()
"""
Example 1.3.8:
函式annotate()
新增圖形內容細節的指向型註釋文字
"""
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls='-',lw='2',c='y',label='plot figure')
plt.legend()
plt.annotate("maximum",
xy=(np.pi/2,1.0),
xytext=((np.pi/2)+1.0,0.8),
weight='bold',
color='b',
arrowprops=
dict(arrowstyle='->',connectionstyle='arc3',color='b')
)
plt.show()
"""
Example 1.3.9:
函式text()
新增圖形內容細節的無指向型註釋文字
"""
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls='-',lw='2',c='y',label='plot figure')
plt.legend()
plt.text(3.1,0.09,'y=sin(x)',weight='bold',color='b')
plt.show()
"""
Example 1.3.10:
函式title()
新增圖形內容的標題
"""
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls='-',lw='2',c='y',label='plot figure')
plt.legend()
plt.title('y=sin(x)')
plt.show()
"""
Example 1.3.11:
函式legend()
標示不同圖形的文字標籤圖例
"""
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls='-',lw='2',c='y',label='plot figure')
plt.legend(loc='upper right')
plt.show()
"""
Example 1.4:
函式組合應用
"""
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm as cm
# define data
x = np.linspace(0.5,3.5,100)
y = np.sin(x)
y1 = np.random.rand(100) * 3
# scatter figure
plt.scatter(x,y1,c='0.25',label='catter figure')
# plot figure
plt.plot(x,y,ls='--',lw=2,label='plot figure')
# set x,yaxis limit
plt.xlim(0.0,4.0)
plt.ylim(-3.0,3.0)
# set axes labels
plt.xlabel('x_axis')
plt.ylabel('y_axis')
# set x,yaxis grid
plt.grid(ls=':',color='r')
# add a horizontal line across the axis
plt.axhline(y=0.0,c='r',ls='--',lw=2)
# add a vertical span across the axis
plt.axvspan(xmin=1.0,xmax=2.0,facecolor='r',alpha=0.3)
# set annotating information
plt.annotate('maximum',xy=(np.pi/2,1.0),
xytext=((np.pi/2)+0.15,1.5),weight='bold',color='r',
arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='r'))
plt.annotate('spines',xy=(0.75,-3),
xytext=(0.35,-2.25),weight='bold',color='r',
arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='r'))
plt.annotate('',xy=(0,-2.78),
xytext=(0.4,-2.32),weight='bold',color='r',
arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='r'))
plt.annotate('',xy=(3.5,-2.98),
xytext=(3.6,-2.7),weight='bold',color='r',
arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='r'))
# set text information
plt.text(3.6,-2.7,"'|' is tickline",weight='bold',color='b')
plt.text(3.6,-2.95,"3.5 is ticklabel",weight='bold',color='b')
# set title
plt.title("structure of matplotlib")
# set legend
plt.legend(loc='upper right')
plt.show()