Data Visualization (a): matplotlib

Introduction 1. matplotlib

Matplotlib is a Python 2D graphics libraries, you may generate quality data of various publications in hardcopy form and cross-platform interactive environment. Matplotlib can be used Python scripting, Python and IPython shell, Jupyter notebook, Web application servers, and four graphical user interface toolkit.

2. matplotlib installation

matplotlib source may be used to install installation and mounting pip. Installation pip follows:

pip install matplotlib

Default install the latest version, you can install the specified version

pip install matplotlib==2.2.0

Example 3. matplotlib Drawing

3.1 exchange system common statistical graphics

  • Scatter
x = np.arange(50)
y = x + 5 * np.random.rand(50)
plt.scatter(x, y)
plt.title('散点图')                             # 添加标题
plt.xlabel('自变量')                         # 添加横坐标
plt.ylabel('因变量')                         # 添加纵坐标
plt.xlim(xmin=0, xmax=50)            # 添加横坐标范围 
plt.ylim(ymin=0, ymax=50)            # 添加纵坐标范围

Data Visualization (a): matplotlib

  • Histogram

    plt.hist(x=np.random.randn(100), bins=10, color='b', alpha=0.3)

    Data Visualization (a): matplotlib

  • line chart

    plt.plot([1,2,3,4,5],[1,4,5,2,7])

    Data Visualization (a): matplotlib

  • Histogram

    x = np.arange(5)
    y1, y2 = np.random.randint(1, 25, size=(2, 5))
    width = 0.25
    plt.bar(x, y1, width, color='r')
    plt.bar(x+width, y2, width, color='g')

    Data Visualization (a): matplotlib

  • Pie
explode=(0,0.1,0,0,0)
partions = [0.30,0.20,0.1,0.15,0.25]
labels = ['苹果','三星','小米','华为','others']
plt.pie(partions,labels=labels,explode=explode,autopct='%1.0f%%')

Data Visualization (a): matplotlib

Department of Mathematical Functions curve prepared 3.2

  • Trigonometric functions

    x = np.arange(-np.pi,np.pi,0.01)
    y1 = np.sin(x)
    y2 = np.cos(x)
    plt.plot(x,y1,color='green',linewidth=1,linestyle='-',label='正弦曲线')
    plt.plot(x,y2,color='blue',linewidth=1,linestyle='--',label='余弦曲线')
    plt.legend()   # 添加标注

    Data Visualization (a): matplotlib

  • Exponential function

    t = np.linspace(-50.0,50.0,1000)
    func_exp = np.exp(-0.1*t)
    plt.plot(t,func_exp)
    plt.title('exp(-0.1*t)')

    Data Visualization (a): matplotlib

  • Logarithmic function
    t = np.linspace(-10.0,10.0,1000)
    func_log2 = np.log2(t)
    plt.plot(t,func_log2)
    plt.title('log2(t)')
    plt.grid()

    Data Visualization (a): matplotlib

Guess you like

Origin blog.51cto.com/12631595/2402946