Python簡單繪圖一__Python

來源:互聯網
上載者:User

其實一直都想學習一下Python,但是程式員都知道,只有當你真正用到一門語言的時候,學起來效率最高,所以我現在要用了

本來這個畫圖的工作,同事已經用MATLAB完成了,但是我自己一直覺得MATLAB不感冒,所以嘗試用Python來做。


例子:http://matplotlib.org/examples/index.html

首先在Ubuntu16.04系統內建了Python(怎麼方便怎麼來)


需要安裝pip


安裝依賴庫




OK 開始操練


1、畫直線

import numpy as npimport matplotlib.pyplot as pltx=[0,1]y=[0,1]plt.figure()plt.plot(x,y)plt.show()#plt.savefig("easyplot.jpg")


2、畫圓

from matplotlib.patches import Ellipse, Circleimport matplotlib.pyplot as pltfig = plt.figure()ax = fig.add_subplot(111)cir1 = Circle(xy = (0.0, 1.0), radius=3, alpha=0.4) ax.add_patch(cir1)plt.axis('scaled')plt.axis('equal') plt.title('circle')plt.show()


3、畫散佈圖(這個就是我目前主要用到的)

from numpy import *;import numpy as npimport matplotlib.pyplot as pltN = 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()

這裡用到一個matplotlib.pyplot子庫中畫散佈圖的函數 
matplotlib.pyplot.scatter(x, y, s=20, c=None, marker='o', 
cmap=None, norm=None, vmin=None, vmax=None, alpha=None, 
linewidths=None, verts=None, edgecolors=None, hold=None, 
data=None, **kwargs)  
這個函數接收的參數很多,有預設值的平時也不需要我們指定,是可選的,這次我們用到的除了基本的x ,y參數,還有c,s,alpha和marker,c就是為點指定的顏色數組,s是點的面積大小,alpha是點的顏色的透明度,marker是指定點標記的形狀。在例子裡指定透明度為0.5,c和s是隨機產生的,我們要改變的是marker的值,marker有很多值可供選擇,下表展示了在例子代碼的基礎上,改變marker的值後的效果:

marker result
”.”
”,”
“o”
“v”
“^”
“<”
“>”
“1”
“2”
“3”
“4”
“8”
“s”
“p”
“*”
“h”
“H”
“+”
“x”
“D”
“d”
“_”
“None” 沒錯就是什麼都沒有。。。
“$…$”
(numsides, style, angle)
eg:(9,0, 30) 
註:numsides是邊的個數,
angle是旋轉角度,
style只有0,1,2,3四個值
(numsides, style, angle)
eg:(9,1, 30)
(numsides, style, angle)
eg:(9,2, 30)
(numsides, style, angle)
eg:(9,3, 30)
註:此時numsides和angle的值自動被忽略


4、各種形狀

import matplotlib.pyplot as pltplt.rcdefaults()import numpy as npimport matplotlib.pyplot as pltimport matplotlib.path as mpathimport matplotlib.lines as mlinesimport matplotlib.patches as mpatchesfrom matplotlib.collections import PatchCollectiondef 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 artistsgrid = np.mgrid[0.2:0.8:3j, 0.2:0.8:3j].reshape(2, -1).Tpatches = []# add a circlecircle = mpatches.Circle(grid[0], 0.1, ec="none")patches.append(circle)label(grid[0], "Circle")# add a rectanglerect = mpatches.Rectangle(grid[1] - [0.025, 0.05], 0.05, 0.1, ec="none")patches.append(rect)label(grid[1], "Rectangle")# add a wedgewedge = mpatches.Wedge(grid[2], 0.1, 30, 270, ec="none")patches.append(wedge)label(grid[2], "Wedge")# add a Polygonpolygon = mpatches.RegularPolygon(grid[3], 5, 0.1)patches.append(polygon)label(grid[3], "Polygon")# add an ellipseellipse = mpatches.Ellipse(grid[4], 0.2, 0.1)patches.append(ellipse)label(grid[4], "Ellipse")# add an arrowarrow = 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 patchPath = mpath.Pathpath_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 boxfancybox = 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 linex, 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()


基於上面的例子,大部分比較基礎的繪圖應該可以完成了,程式猿最擅長的就是照貓畫虎,舉一反三


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.