I finally know how to use matplotlib be drawing

Excerpt: https://www.cnblogs.com/zszxz/p/12149330.html

I. Introduction

Getting started with this article you how to use matplotlib draw beautiful pictures of mathematics; after reading this article you will get familiar with and use matplotlib simple tool, will have to draw the basic discount, scatter, sin, cos map, a Zhang canvas to draw more map etc. Of course matplotlib have to do much more than that, the reader can refer to the official website to learn.

Second image was composed

The following pictures from matplotlib official website, briefly explain the picture was composed;

  1. figure: canvas picture was overall profile
  2. Axes: can draw multiple images on a number of axes, a canvas
  3. axis: axis, typically have an x-axis, y-axis, etc.
  4. tick: graduated scale too, the coordinate axes
  5. title: to give the title picture
  6. legend: Legend
  7. grid: Grid
  8. label: Label Description

Three paintings discounting map

Before drawing matplotlib to import numpy libraries and library;

3.1 simple line chart

Videos have a simple line graph, the input values ​​have to be variable y, and set x, y-axis label and font size to give a detailed description see Listing

import matplotlib.pyplot as plot
import numpy as np
# 着线图数据 line = [1,2,6,8,9,15,23,29,35] # 指定线条粗细 plot.plot(line,linewidth=2) # 设置标题 plot.title("zszxz line ",fontsize=12) # 设置x轴 plot.xlabel("x",fontsize=12) # 设置y轴 plot.ylabel("y",fontsize=12) # 显示 plot.show()

Pictures show results

3.2 full-line graph

The following is a painted image obtained with x, y discounted FIG worth number, the ability to customize the x, y, the mathematical digital image effect transducer; drawing coordinate is (1,2), (2,4), ( 6,12), (8,16), (9,18); value y is exactly twice (y = 2x) x; and can also be found in the previous picture is different from the initial value is not (0 , 0), but (2);

import matplotlib.pyplot as plot
import numpy as np
# 着线图数据 line1 = [1,2,6,8,9] line2 = [2,4,12,16,18] # 指定线条粗细 plot.plot(line1, line2,linewidth=2) # 设置标题 plot.title("zszxz line ",fontsize=12) # 设置x轴 plot.xlabel("x",fontsize=12) # 设置y轴 plot.ylabel("y",fontsize=12) # 显示 plot.show()

Image results

3.3 discount sales chart

Simulation of a fruit sales each month, for example two knowledge seeker fruit are apples, bananas, and then displays the discount sales for each month by FIG; Listing legend information is set to restrict the range of the x axis (1 ~ December), x axis scale is provided;

import matplotlib.pyplot as plot
import numpy as np
# 折线图数据
line1 = [1,2,6,8,9,25,41,65,32,14,45,100] line2 = [2,4,12,16,18,45,3,4,55,67,78,22] # 线条1 plot.plot(line1,color='red',label="apple",linewidth=2) # 线条2 plot.plot(line2,color='blue',label="banana",linewidth=2) # 设置x轴 plot.xlabel("month",fontsize=12) # 设置y轴 plot.ylabel("sales",fontsize=12) # 设置标题 plot.title("zszxz prodcut sale ",fontsize=12) # 添加图例 plot.legend() # 设置x范围 plot.xlim(1,12) # 设置 x 刻度 x_ticks = np.arange(1, 13, 1) plot.xticks(x_ticks) # 显示 plot.show()

Results of the

Four paintings Scatter

4.1 simple scatter plot

We will simply (x, y) coordinate matching, then draw on the canvas in a simple scattergram;

import matplotlib.pyplot as plot
import numpy as np
x = [1,3,5,7,9] y = [2,4,6,8,10] # 散点 plot.scatter(x,y) # 设置标题 plot.title("zszxz scatter",fontsize=12) # 设置x轴 plot.xlabel("x",fontsize=12) # 设置y轴 plot.ylabel("y",fontsize=12) # 显示 plot.show()

The results below, you can see the basic realization of the scatter plot function but is not very nice;

4.2 computational Scatter

实际应用中不可能要我们手动输入2个列表进行描绘散点图,一般都包含一种数学关系,我们用计算的方式获得散点图;

import matplotlib.pyplot as plot
import numpy as np
x_val = list(range(1,50)) # y的值是x的平方 y_val = [x**2 for x in x_val] # 设置x轴 plot.xlabel("x",fontsize=12) # 设置y轴 plot.ylabel("y",fontsize=12) # 散点 plot.scatter(x_val,y_val,color='pink') # 显示 plot.show()

执行结果如下,修改颜色,数据集变多之后,明显就好看了许多;

4.3 随机数散点图

这次我们将使用随机数进行描绘图片,并设置图片散点颜色,标记属性;marker 属性默认是 o ,圆点,我们将其改成 * ; 更多marker配置配置可以参照官方文档

import matplotlib.pyplot as plot
import numpy as np
# 500 个随机数 n = 500 # 平均值为0,方差为1 X = np.random.normal(0,1,n) Y = np.random.normal(0,1,n) #plot.scatter(X,Y,color='green',alpha=0.7) plot.scatter(X,Y,color='green',alpha=0.7,marker='*') #设置网格线 #plot.grid(True) plot.show()

执行结果如下,可以发现瞬间美观了许多,当然根据不同的实际需求,画出的图片也不一样。

五 画正余弦

5.1正弦

知识追寻者将画 一个 x 轴从-2Π 到 + 2 Π的 正弦图,并且在区间进行1024个等分处理;

import matplotlib.pyplot as plot
import numpy as np
# -2Π 到 2 Π 1024 个等值 x = np.linspace(-np.pi*2, np.pi*2, 1024) y = np.sin(x) plot.plot(x, y,c='r') # 设置x轴 plot.xlabel("x",fontsize=12) # 设置y轴 plot.ylabel("y=sin(x)",fontsize=12) # 设置标题 plot.title("zszxz sin",fontsize=12) # 显示 plot.show()

执行结果如下:

5.2 余弦

跟正弦类似,画 一个 x 轴从-2Π 到 + 2 Π的 正弦图,并且在区间进行1024个等分处理;

import matplotlib.pyplot as plot
import numpy as np
# -2Π 到 2 Π 1024 个等值 x = np.linspace(-np.pi*2, np.pi*2, 1024) y = np.cos(x) plot.plot(x, y,c='c') # 设置x轴 plot.xlabel("x",fontsize=12) # 设置y轴 plot.ylabel("y=cons(x)",fontsize=12) # 设置标题 plot.title("zszxz cos",fontsize=12) # 显示 plot.show()

六 画布多图

知识追寻者将5章节的正弦,余弦整合,然后在一张画布上画出2张图片;懂得这个方法之后,读者可以在任意得一张画布上画出多张图片;为了显示更加好看,还设置了虚线(dashes)

import matplotlib.pyplot as plot
import numpy as np
# 创建画布 plot.figure() # 子图1 plot.subplot(211) x = np.linspace(-np.pi*2, np.pi*2, 1024) sin_y = np.sin(x) # 设置虚线 plot.plot(x, sin_y,dashes=[6, 3],c='m') # 子图2 plot.subplot(212) con_y = np.cos(x) plot.plot(x, con_y,dashes=[6, 2],c='r') # 显示 plot.show()

 
分类:  python

一 前言

本篇文章带大家快速入门如何使用matplotlib画出精美数学的图片;看完本篇文章你将获得熟悉并简单使用matplotlib工具,会画基本得折现图,散点图,sin,cos图,一张画布画出多图等. 当然matplotlib得功能远不止这些,读者可以参照官网进行学习。

二 图像得组成

下面张图片来自matplotlib官网,简单说明一下图片得组成;

  1. figure:画布,一张图片得整体轮廓
  2. Axes:数轴,一张画布上可以画多张图片
  3. axis:坐标轴,通常得x轴,y轴等
  4. tick:刻度,坐标轴上得刻度
  5. title: 图片得标题
  6. legend:图例
  7. grid: 网格
  8. label:标签说明

三 画折现图

画图之前要导入matplotlib库和numpy库;

3.1 简单折线图

画一张简单得折线图,输入得数值只有y变量,并且设置x,y轴得标签和字体大小,详细说明看代码清单

import matplotlib.pyplot as plot
import numpy as np
# 着线图数据 line = [1,2,6,8,9,15,23,29,35] # 指定线条粗细 plot.plot(line,linewidth=2) # 设置标题 plot.title("zszxz line ",fontsize=12) # 设置x轴 plot.xlabel("x",fontsize=12) # 设置y轴 plot.ylabel("y",fontsize=12) # 显示 plot.show()

图片显示结果

3.2 齐全的折线图

下面得图片画得是带有x,y数值得折现图,能够自定义x,y值,实现数学上的数字换图片效果; 描绘坐标就是(1,2),(2,4),(6,12),(8,16),(9,18);y的值正好是x的2倍(y = 2x);也可以发现与前面一张图片的不同之处就是初始值不是(0,0)开始,而是(1,2);

import matplotlib.pyplot as plot
import numpy as np
# 着线图数据 line1 = [1,2,6,8,9] line2 = [2,4,12,16,18] # 指定线条粗细 plot.plot(line1, line2,linewidth=2) # 设置标题 plot.title("zszxz line ",fontsize=12) # 设置x轴 plot.xlabel("x",fontsize=12) # 设置y轴 plot.ylabel("y",fontsize=12) # 显示 plot.show()

图片结果

3.3 销售折现图

模拟一次水果每个月份的销量,知识追寻者举例2个水果,分别是苹果,香蕉,然后用折现图显示每个月份的销量;代码清单将设置图例信息,限制x轴的范围(1~12月),设置x轴刻度;

import matplotlib.pyplot as plot
import numpy as np
# 折线图数据
line1 = [1,2,6,8,9,25,41,65,32,14,45,100] line2 = [2,4,12,16,18,45,3,4,55,67,78,22] # 线条1 plot.plot(line1,color='red',label="apple",linewidth=2) # 线条2 plot.plot(line2,color='blue',label="banana",linewidth=2) # 设置x轴 plot.xlabel("month",fontsize=12) # 设置y轴 plot.ylabel("sales",fontsize=12) # 设置标题 plot.title("zszxz prodcut sale ",fontsize=12) # 添加图例 plot.legend() # 设置x范围 plot.xlim(1,12) # 设置 x 刻度 x_ticks = np.arange(1, 13, 1) plot.xticks(x_ticks) # 显示 plot.show()

执行结果

四 画散点图

4.1 简单的散点图

我们将以简单的(x,y)坐标匹配,然后在画布上画出简单的散点图;

import matplotlib.pyplot as plot
import numpy as np
x = [1,3,5,7,9] y = [2,4,6,8,10] # 散点 plot.scatter(x,y) # 设置标题 plot.title("zszxz scatter",fontsize=12) # 设置x轴 plot.xlabel("x",fontsize=12) # 设置y轴 plot.ylabel("y",fontsize=12) # 显示 plot.show()

执行结果如下,可以看见基本实现了散点图功能不过不是很好看;

4.2 计算型散点图

实际应用中不可能要我们手动输入2个列表进行描绘散点图,一般都包含一种数学关系,我们用计算的方式获得散点图;

import matplotlib.pyplot as plot
import numpy as np
x_val = list(range(1,50)) # y的值是x的平方 y_val = [x**2 for x in x_val] # 设置x轴 plot.xlabel("x",fontsize=12) # 设置y轴 plot.ylabel("y",fontsize=12) # 散点 plot.scatter(x_val,y_val,color='pink') # 显示 plot.show()

执行结果如下,修改颜色,数据集变多之后,明显就好看了许多;

4.3 随机数散点图

这次我们将使用随机数进行描绘图片,并设置图片散点颜色,标记属性;marker 属性默认是 o ,圆点,我们将其改成 * ; 更多marker配置配置可以参照官方文档

import matplotlib.pyplot as plot
import numpy as np
# 500 个随机数 n = 500 # 平均值为0,方差为1 X = np.random.normal(0,1,n) Y = np.random.normal(0,1,n) #plot.scatter(X,Y,color='green',alpha=0.7) plot.scatter(X,Y,color='green',alpha=0.7,marker='*') #设置网格线 #plot.grid(True) plot.show()

执行结果如下,可以发现瞬间美观了许多,当然根据不同的实际需求,画出的图片也不一样。

五 画正余弦

5.1正弦

知识追寻者将画 一个 x 轴从-2Π 到 + 2 Π的 正弦图,并且在区间进行1024个等分处理;

import matplotlib.pyplot as plot
import numpy as np
# -2Π 到 2 Π 1024 个等值 x = np.linspace(-np.pi*2, np.pi*2, 1024) y = np.sin(x) plot.plot(x, y,c='r') # 设置x轴 plot.xlabel("x",fontsize=12) # 设置y轴 plot.ylabel("y=sin(x)",fontsize=12) # 设置标题 plot.title("zszxz sin",fontsize=12) # 显示 plot.show()

执行结果如下:

5.2 余弦

跟正弦类似,画 一个 x 轴从-2Π 到 + 2 Π的 正弦图,并且在区间进行1024个等分处理;

import matplotlib.pyplot as plot
import numpy as np
# -2Π 到 2 Π 1024 个等值 x = np.linspace(-np.pi*2, np.pi*2, 1024) y = np.cos(x) plot.plot(x, y,c='c') # 设置x轴 plot.xlabel("x",fontsize=12) # 设置y轴 plot.ylabel("y=cons(x)",fontsize=12) # 设置标题 plot.title("zszxz cos",fontsize=12) # 显示 plot.show()

六 画布多图

知识追寻者将5章节的正弦,余弦整合,然后在一张画布上画出2张图片;懂得这个方法之后,读者可以在任意得一张画布上画出多张图片;为了显示更加好看,还设置了虚线(dashes)

import matplotlib.pyplot as plot
import numpy as np
# 创建画布 plot.figure() # 子图1 plot.subplot(211) x = np.linspace(-np.pi*2, np.pi*2, 1024) sin_y = np.sin(x) # 设置虚线 plot.plot(x, sin_y,dashes=[6, 3],c='m') # 子图2 plot.subplot(212) con_y = np.cos(x) plot.plot(x, con_y,dashes=[6, 2],c='r') # 显示 plot.show()

Guess you like

Origin www.cnblogs.com/xichji/p/12154647.html