python绘图

原地址: https://blog.csdn.net/lucky_greenegg/article/details/77109484

 

1、画直线

  1. import numpy as np

  2. import matplotlib.pyplot as plt

  3.  
  4. x=[0,1]

  5. y=[0,1]

  6. plt.figure()

  7. plt.plot(x,y)

  8. plt.show()

  9. #plt.savefig("easyplot.jpg")


 

 

2、画圆

 

 
  1. from matplotlib.patches import Ellipse, Circle

  2.  
  3. import matplotlib.pyplot as plt

  4. fig = plt.figure()

  5. ax = fig.add_subplot(111)

  6. cir1 = Circle(xy = (0.0, 1.0), radius=3, alpha=0.4)

  7. ax.add_patch(cir1)

  8. plt.axis('scaled')

  9. plt.axis('equal')

  10. plt.title('circle')

  11. plt.show()


 

 

3、画散点图(这个就是我目前主要用到的)

 

 
  1. from numpy import *;

  2. import numpy as np

  3. import matplotlib.pyplot as plt

  4.  
  5. N = 50

  6. x = np.random.rand(N)

  7. y = np.random.rand(N)

  8. colors = np.random.rand(N)

  9. area = np.pi * (15 * np.random.rand(N))**2

  10. plt.scatter(x, y, s=area, c=colors, alpha=0.5, marker=(9, 3, 30))

  11. 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的值后的效果:

 

 

markerresult
”.”
”,”
“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、各种形状

 

 
  1. import matplotlib.pyplot as plt

  2. plt.rcdefaults()

  3.  
  4. import numpy as np

  5. import matplotlib.pyplot as plt

  6. import matplotlib.path as mpath

  7. import matplotlib.lines as mlines

  8. import matplotlib.patches as mpatches

  9. from matplotlib.collections import PatchCollection

  10.  
  11.  
  12. def label(xy, text):

  13. y = xy[1] - 0.15 # shift y-value for label so that it's below the artist

  14. plt.text(xy[0], y, text, ha="center", family='sans-serif', size=14)

  15.  
  16.  
  17. fig, ax = plt.subplots()

  18. # create 3x3 grid to plot the artists

  19. grid = np.mgrid[0.2:0.8:3j, 0.2:0.8:3j].reshape(2, -1).T

  20.  
  21. patches = []

  22.  
  23. # add a circle

  24. circle = mpatches.Circle(grid[0], 0.1, ec="none")

  25. patches.append(circle)

  26. label(grid[0], "Circle")

  27.  
  28. # add a rectangle

  29. rect = mpatches.Rectangle(grid[1] - [0.025, 0.05], 0.05, 0.1, ec="none")

  30. patches.append(rect)

  31. label(grid[1], "Rectangle")

  32.  
  33. # add a wedge

  34. wedge = mpatches.Wedge(grid[2], 0.1, 30, 270, ec="none")

  35. patches.append(wedge)

  36. label(grid[2], "Wedge")

  37.  
  38. # add a Polygon

  39. polygon = mpatches.RegularPolygon(grid[3], 5, 0.1)

  40. patches.append(polygon)

  41. label(grid[3], "Polygon")

  42.  
  43. # add an ellipse

  44. ellipse = mpatches.Ellipse(grid[4], 0.2, 0.1)

  45. patches.append(ellipse)

  46. label(grid[4], "Ellipse")

  47.  
  48. # add an arrow

  49. arrow = mpatches.Arrow(grid[5, 0] - 0.05, grid[5, 1] - 0.05, 0.1, 0.1, width=0.1)

  50. patches.append(arrow)

  51. label(grid[5], "Arrow")

  52.  
  53. # add a path patch

  54. Path = mpath.Path

  55. path_data = [

  56. (Path.MOVETO, [0.018, -0.11]),

  57. (Path.CURVE4, [-0.031, -0.051]),

  58. (Path.CURVE4, [-0.115, 0.073]),

  59. (Path.CURVE4, [-0.03 , 0.073]),

  60. (Path.LINETO, [-0.011, 0.039]),

  61. (Path.CURVE4, [0.043, 0.121]),

  62. (Path.CURVE4, [0.075, -0.005]),

  63. (Path.CURVE4, [0.035, -0.027]),

  64. (Path.CLOSEPOLY, [0.018, -0.11])

  65. ]

  66. codes, verts = zip(*path_data)

  67. path = mpath.Path(verts + grid[6], codes)

  68. patch = mpatches.PathPatch(path)

  69. patches.append(patch)

  70. label(grid[6], "PathPatch")

  71.  
  72. # add a fancy box

  73. fancybox = mpatches.FancyBboxPatch(

  74. grid[7] - [0.025, 0.05], 0.05, 0.1,

  75. boxstyle=mpatches.BoxStyle("Round", pad=0.02))

  76. patches.append(fancybox)

  77. label(grid[7], "FancyBboxPatch")

  78.  
  79. # add a line

  80. x, y = np.array([[-0.06, 0.0, 0.1], [0.05, -0.05, 0.05]])

  81. line = mlines.Line2D(x + grid[8, 0], y + grid[8, 1], lw=5., alpha=0.3)

  82. label(grid[8], "Line2D")

  83.  
  84. colors = np.linspace(0, 1, len(patches))

  85. collection = PatchCollection(patches, cmap=plt.cm.hsv, alpha=0.3)

  86. collection.set_array(np.array(colors))

  87. ax.add_collection(collection)

  88. ax.add_line(line)

  89.  
  90. plt.subplots_adjust(left=0, right=1, bottom=0, top=1)

  91. plt.axis('equal')

  92. plt.axis('off')

  93.  
  94. plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值