python基础,安装并使用matplotlib库画图

在学校时,常常使用 matlab,觉得它的一大好处就是画图非常方便,可以随时将关心的数据以图表的形式表现出来。现在经常用 python,也想把关心的数据,绘制成图表,怎么处理呢?

安装 matplotlib 库


python 使用 matplotlib 库绘制图表非常方便,其安装过程,这里以 ubuntu 操作系统为例,只需执行:

sudo apt-get install python-matplotlib

即可。其安装命令不是 pip install,从这点可以看出,绘制图表应该是跟操作系统相关的,也就是说,应该需要操作系统支持图表显示,当然,这只是我的猜测,我的 ubuntu 是带界面的。

使用 matplotlib 库


1. 二维描点作图

是最基础的一种使用,即:

import matplotlib.pyplot as plt  

x=[0,1]  
y=[0,1]  
plt.figure()  
plt.plot(x,y)  
plt.show()  
#plt.savefig("easyplot.jpg")  

执行结果如下:

很像 matlab 的 plot 函数画出的图。

2. 画散点图

直接上代码:

import numpy as np  
import matplotlib.pyplot as plt  

N = 50   
x = np.random.rand(N)           
y = np.random.rand(N)       # 随机位置
colors = np.random.rand(N)  # 随机颜色
area = np.pi * (15 * np.random.rand(N))**2      # 随机大小
plt.scatter(x, y, s=area, c=colors, alpha=0.5, marker=(9, 3, 30))  
plt.show() 

执行结果如下:

主要用到 scatter 函数,它的各个参数意义应该也非常清楚。

3. 画各种图

import matplotlib.pyplot as plt  
plt.rcdefaults()  

import numpy as np  
import matplotlib.pyplot as plt  
import matplotlib.path as mpath  
import matplotlib.lines as mlines  
import matplotlib.patches as mpatches  
from matplotlib.collections import PatchCollection  


def label(xy, text):  
    y = xy[1] - 0.15  # shift y-value for label so that it's below the artist  
    plt.text(xy[0], y, text, ha="center", family='sans-serif', size=14)  


fig, ax = plt.subplots()  
# create 3x3 grid to plot the artists  
grid = np.mgrid[0.2:0.8:3j, 0.2:0.8:3j].reshape(2, -1).T  

patches = []  

# add a circle  
circle = mpatches.Circle(grid[0], 0.1, ec="none")  
patches.append(circle)  
label(grid[0], "Circle")  

# add a rectangle  
rect = mpatches.Rectangle(grid[1] - [0.025, 0.05], 0.05, 0.1, ec="none")  
patches.append(rect)  
label(grid[1], "Rectangle")  

# add a wedge  
wedge = mpatches.Wedge(grid[2], 0.1, 30, 270, ec="none")  
patches.append(wedge)  
label(grid[2], "Wedge")  

# add a Polygon  
polygon = mpatches.RegularPolygon(grid[3], 5, 0.1)  
patches.append(polygon)  
label(grid[3], "Polygon")  

# add an ellipse  
ellipse = mpatches.Ellipse(grid[4], 0.2, 0.1)  
patches.append(ellipse)  
label(grid[4], "Ellipse")  

# add an arrow  
arrow = mpatches.Arrow(grid[5, 0] - 0.05, grid[5, 1] - 0.05, 0.1, 0.1, width=0.1)  
patches.append(arrow)  
label(grid[5], "Arrow")  

# add a path patch  
Path = mpath.Path  
path_data = [  
    (Path.MOVETO, [0.018, -0.11]),  
    (Path.CURVE4, [-0.031, -0.051]),  
    (Path.CURVE4, [-0.115,  0.073]),  
    (Path.CURVE4, [-0.03 ,  0.073]),  
    (Path.LINETO, [-0.011,  0.039]),  
    (Path.CURVE4, [0.043,  0.121]),  
    (Path.CURVE4, [0.075, -0.005]),  
    (Path.CURVE4, [0.035, -0.027]),  
    (Path.CLOSEPOLY, [0.018, -0.11])  
    ]  
codes, verts = zip(*path_data)  
path = mpath.Path(verts + grid[6], codes)  
patch = mpatches.PathPatch(path)  
patches.append(patch)  
label(grid[6], "PathPatch")  

# add a fancy box  
fancybox = mpatches.FancyBboxPatch(  
    grid[7] - [0.025, 0.05], 0.05, 0.1,  
    boxstyle=mpatches.BoxStyle("Round", pad=0.02))  
patches.append(fancybox)  
label(grid[7], "FancyBboxPatch")  

# add a line  
x, y = np.array([[-0.06, 0.0, 0.1], [0.05, -0.05, 0.05]])  
line = mlines.Line2D(x + grid[8, 0], y + grid[8, 1], lw=5., alpha=0.3)  
label(grid[8], "Line2D")  

colors = np.linspace(0, 1, len(patches))  
collection = PatchCollection(patches, cmap=plt.cm.hsv, alpha=0.3)  
collection.set_array(np.array(colors))  
ax.add_collection(collection)  
ax.add_line(line)  

plt.subplots_adjust(left=0, right=1, bottom=0, top=1)  
plt.axis('equal')  
plt.axis('off')  

plt.show()  

执行结果如下:

本帖仅仅做个笔记,是转载的,原帖:

https://blog.csdn.net/lucky_greenegg/article/details/77109484

阅读更多:   Python
暂无评论
  1. […] 可以参照python基础,安装并使用matplotlib库画图小节,把该直线画出来,有个直观感受。python 代码如下: […]

  2. […] 利用 matplotlib 库将生成的数据绘制成散点图(matplotlib 库的使用,可参考:python基础,安装并使用matplotlib库画图),添加代码: […]

添加新评论

icon_redface.gificon_idea.gificon_cool.gif2016kuk.gificon_mrgreen.gif2016shuai.gif2016tp.gif2016db.gif2016ch.gificon_razz.gif2016zj.gificon_sad.gificon_cry.gif2016zhh.gificon_question.gif2016jk.gif2016bs.gificon_lol.gif2016qiao.gificon_surprised.gif2016fendou.gif2016ll.gif