matplotlib draws a simple scatter plot

Today learned to draw a scatter plot with python third-party libraries matplotlib, mainly matplotlib.pyplot ()
function. (Hereinafter referred to as plt)

  • plt.scatter (x, y, c, cmap, edgecolor, s): This function is mainly plotted points you specify.
    1. Parameter x represents the abscissa of the point (the xlabel);
    2. the parameter y represents the vertical coordinate point (ylabel);
    3. Parameter c represents the color (color) dots;
    4. cmap parameter which tells pyplot color map using ( colormap);

E.g:

plt.scatter(x, y, c=y, cmap=plt.cm.Blues)
"""此时x, y均是列表,
我们将参数c设置成关于y值的列表,
而plt.cm将告诉pyplot用哪种颜色
蓝色:plt.cm.Blues
红色:plt.cm.Reds
以此类推"""

5 parameters edgecolor, by definition, represents the color of edge points (edge color if undesirable, can be set to "none";
. 6 tells the size of the parameters, s pyplot point
(Note:
1 addition to the parameters x, y, the other parameters are optional
2 pyplot all colors are mapped in the official website http://matplotlib.org/, access, single Examples, Examples scroll down to color.
3 the X-, the y-list items can not be too much, not too big numbers )

  • plt.axis ([]): the parameter is a list to be provided, for determining the x, y-axis range
    (Note: if the input value is too large, there may not be the results you want)
    e.g.
plt.axis([0, 100, 0, 100])       #第一组值是x轴取值范围,第二组是y轴的

Color Mapping
Input source code is as follows:

import matplotlib.pyplot as plt

value_x = list(range(1, 1001))
value_y = [v**2  for v in value_x]

plt.scatter(value_x, value_y, c=value_y, cmap=plt.cm.Blues, s=40)
#如果想取消点的边缘颜色,上面有介绍
plt.title("Square Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square Value", fontsize=14)

plt.tick_params(labelsize=14)
plt.axis([0, 1100, 0, 1100000])
plt.show()
  • If you want to automatically save the last chart, you can add plt.savefig plt.show () before ()
  • plt.savefig (name, bbox_inches = "tight"): parameter name is the name of the list to be saved; bbox_inches = "tight" around the delete icon for more than a blank, if you do not delete, you can not add.
Released seven original articles · won praise 1 · views 152

Guess you like

Origin blog.csdn.net/m0_46236946/article/details/104149641