Python数据可视化之matplotlib实践chapter-01

"""
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()

 

猜你喜欢

转载自blog.csdn.net/weixin_41526797/article/details/85029590
今日推荐