当前位置:   article > 正文

Python画图之散点图(plt.scatter)_python plt.scatter

python plt.scatter

        散点图的应用很广泛,以前介绍过很多画图方法:Python画图(直方图、多张子图、二维图形、三维图形以及图中图),漏掉了这个,现在补上,用法很简单,我们可以help(plt.scatter)看下它的用法:

  1. Help on function scatter in module matplotlib.pyplot:
  2. scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None,
  3. vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=None, **kwargs)
  4. Make a scatter plot of `x` vs `y`
  5. Marker size is scaled by `s` and marker color is mapped to `c`
  6. Parameters
  7. ----------
  8. x, y : array_like, shape (n, )
  9. Input data
  10. s : scalar or array_like, shape (n, ), optional
  11. size in points^2. Default is `rcParams['lines.markersize'] ** 2`.
  12. c : color, sequence, or sequence of color, optional, default: 'b'
  13. `c` can be a single color format string, or a sequence of color
  14. specifications of length `N`, or a sequence of `N` numbers to be
  15. mapped to colors using the `cmap` and `norm` specified via kwargs
  16. (see below). Note that `c` should not be a single numeric RGB or
  17. RGBA sequence because that is indistinguishable from an array of
  18. values to be colormapped. `c` can be a 2-D array in which the
  19. rows are RGB or RGBA, however, including the case of a single
  20. row to specify the same color for all points.
  21. marker : `~matplotlib.markers.MarkerStyle`, optional, default: 'o'
  22. See `~matplotlib.markers` for more information on the different
  23. styles of markers scatter supports. `marker` can be either
  24. an instance of the class or the text shorthand for a particular
  25. marker.
  26. cmap : `~matplotlib.colors.Colormap`, optional, default: None
  27. A `~matplotlib.colors.Colormap` instance or registered name.
  28. `cmap` is only used if `c` is an array of floats. If None,
  29. defaults to rc `image.cmap`.
  30. norm : `~matplotlib.colors.Normalize`, optional, default: None
  31. A `~matplotlib.colors.Normalize` instance is used to scale
  32. luminance data to 0, 1. `norm` is only used if `c` is an array of
  33. floats. If `None`, use the default :func:`normalize`.
  34. vmin, vmax : scalar, optional, default: None
  35. `vmin` and `vmax` are used in conjunction with `norm` to normalize
  36. luminance data. If either are `None`, the min and max of the
  37. color array is used. Note if you pass a `norm` instance, your
  38. settings for `vmin` and `vmax` will be ignored.
  39. alpha : scalar, optional, default: None
  40. The alpha blending value, between 0 (transparent) and 1 (opaque)
  41. linewidths : scalar or array_like, optional, default: None
  42. If None, defaults to (lines.linewidth,).
  43. verts : sequence of (x, y), optional
  44. If `marker` is None, these vertices will be used to
  45. construct the marker. The center of the marker is located
  46. at (0,0) in normalized units. The overall marker is rescaled
  47. by ``s``.
  48. edgecolors : color or sequence of color, optional, default: None
  49. If None, defaults to 'face'
  50. If 'face', the edge color will always be the same as
  51. the face color.
  52. If it is 'none', the patch boundary will not
  53. be drawn.
  54. For non-filled markers, the `edgecolors` kwarg
  55. is ignored and forced to 'face' internally.
  56. Returns
  57. -------
  58. paths : `~matplotlib.collections.PathCollection`
  59. Other parameters
  60. ----------------
  61. kwargs : `~matplotlib.collections.Collection` properties
  62. See Also
  63. --------
  64. plot : to plot scatter plots when markers are identical in size and
  65. color
  66. Notes
  67. -----
  68. * The `plot` function will be faster for scatterplots where markers
  69. don't vary in size or color.
  70. * Any or all of `x`, `y`, `s`, and `c` may be masked arrays, in which
  71. case all masks will be combined and only unmasked points will be
  72. plotted.
  73. Fundamentally, scatter works with 1-D arrays; `x`, `y`, `s`, and `c`
  74. may be input as 2-D arrays, but within scatter they will be
  75. flattened. The exception is `c`, which will be flattened only if its
  76. size matches the size of `x` and `y`.

我们可以看到参数比较多,平时主要用到的就是大小、颜色、样式这三个参数

s:形状的大小,默认 20,也可以是个数组,数组每个参数为对应点的大小,数值越大对应的图中的点越大。
c:形状的颜色,"b":blue   "g":green    "r":red   "c":cyan(蓝绿色,青色)  "m":magenta(洋红色,品红色) "y":yellow "k":black  "w":white
marker:常见的形状有如下
".":点                   ",":像素点           "o":圆形
"v":朝下三角形   "^":朝上三角形   "<":朝左三角形   ">":朝右三角形
"s":正方形           "p":五边星          "*":星型
"h":1号六角形     "H":2号六角形 

"+":+号标记      "x":x号标记
"D":菱形              "d":小型菱形 
"|":垂直线形         "_":水平线形

我们来看几个示例(在一张图显示了)

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import pandas as pd
  4. x=np.array([3,5])
  5. y=np.array([7,8])
  6. x1=np.random.randint(10,size=(25,))
  7. y1=np.random.randint(10,size=(25,))
  8. plt.scatter(x,y,c='r')
  9. plt.scatter(x1,y1,s=100,c='b',marker='*')
  10. #使用pandas来读取
  11. x2=[]
  12. y2=[]
  13. rdata=pd.read_table('1.txt',header=None)
  14. for i in range(len(rdata[0])):
  15. x2.append(rdata[0][i].split(',')[0])
  16. y2.append(rdata[0][i].split(',')[1])
  17. plt.scatter(x2,y2,s=200,c='g',marker='o')
  18. plt.show()

 其中文档1.txt内容如下(上面图中的4个绿色大点)

5,6
7,9
3,4
2,7

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号