matplotlib drawing skills Detailed (b)

table of Contents

   1, color, and line type setting point flag
   2, provided the transparency
   3, the legend set
   4, set the grid

1, color, and line type setting point flag

1) Common parameter name: parentheses are abbreviations
  • color ©: line color.
  • linestyle (ls): line shape.
  • linewidth (lw): line width.
  • marker: dot mark shape.
  • markersize (ms): dot size of the marks.
  • markeredgecolor (mec): the edge of the color point.
  • markeredgewidth (mew): point edge width.
  • markerfacecolor (mfc): color point.
① Examples below
plt.plot([1, 2, 3, 4], [5, 6, 7, 8], c="g", ls="--", lw=2, 
          marker="o", ms=8, mec="g", mew=5, mfc="r")

The results are as follows:
Here Insert Picture Description

2) color dot marks can be used with a linear parameter
① format
  • Format: color point marker style line style
  • Note: you do not need to add anything else between the three;
  • For example: ro-
② common color, line type and point mark
  • Color: Blue "b" green "g" red "r" cyan "c" magenta "m" yellow "y" black "k" white "w"
  • Point markers: dot pixel "," circle "o" square "s" triangle "^" "."
  • Linear: linear "-" broken line "-" dot line ":" stippling "-."

For example as follows:

plt.plot([1,2,3,4,5], [2,4,6,8,10], "m^:")

The results are as follows:
Here Insert Picture Description

3) color, line style and marker points Encyclopedia
① color Daquan

Here Insert Picture Description

② line style Daquan
  • '-' linear
  • '-' dashed line
  • '-.' Dotted line
  • ':' Dotted line
③ point marker Encyclopedia

Here Insert Picture Description

2, transparency settings

1) Description

  When drawing the image, we can control the transparency of the image through the alpha parameter, a value between 0 and 1. 0 is completely transparent, an opaque.

2) illustrates
plt.plot([1,3,5,7],[4,9,6,8],marker="s",alpha=0.2)
plt.plot([1,2,3,4,5], [2,4,6,8,10],marker="o",alpha=0.5)

The results are as follows:
Here Insert Picture Description

3, Legend settings

1) the role of legend

  在绘制多条线时,可以设置图例来标注每条线所代表的含义,使图形更加清晰易懂。关于图例设置可以参考如下文章:https://blog.csdn.net/chichoxian/article/details/101058046

2)plt.legend()设置图例的2种方式
① legend函数中的常用参数
  • loc:指定图例的位置。默认为best。也可以指定坐标(元组),基于图像左下角计算。
  • title:设置图例的标题。
  • ncol:图例显示的列数,默认为1列。
  • frameon:设置是否显示图例的边框。True(默认值)显示,False不显示。
  • 例如:plt.legend(loc=(1,0.9))传入一个指定坐标,调整图例的位置,以免图例覆盖了图形。
② 调用plt的legend函数,传递一个标签数组,指定每次plot图形的标签
plt.plot([1,3,5,7],[4,9,6,8],"ro--")
plt.plot([1,2,3,4,5], [2,4,6,8,10],"gs-.")
plt.legend(["2016年","2017年"],loc="best")

结果如下:
Here Insert Picture Description

③ 在绘制的时候通过label参数指定图例中显示的名称,然后调用legend函数生成图例
plt.plot([1,3,5,7],[4,9,6,8],"ro--",label="2016年")
plt.plot([1,2,3,4,5], [2,4,6,8,10],"gs-.",label="2017年")
plt.legend(loc="upper right")

结果如下:
Here Insert Picture Description

3)legend函数常用参数的详细说明
  • loc:指定图例的位置。默认为best。也可以指定坐标(元组),基于图像左下角计算。
  • title:设置图例的标题。
  • ncol:图例显示的列数,默认为1列。
  • frameon:设置是否显示图例的边框。True(默认值)显示,False不显示。
① loc参数:设置图例的摆放位置

  该参数用于指定图例的摆放位置。默认是best,还有upper、down、left和right。一共有四种组合形式"upper left"、“upper right”、“down left"和"down right”。也可以为该参数指定一个坐标"元组",坐标的值是基于当前坐标原点的比例。

举例如下:

plt.plot([1,3,5,7],[4,9,6,8],"ro--")
plt.plot([1,2,3,4,5], [2,4,6,8,10],"gs-.")
plt.legend(["2016年","2017年"],loc=(0.5,0.8))

结果如下:
Here Insert Picture Description

② title参数:为图例设置标题

举例如下:

plt.plot([1,3,5,7],[4,9,6,8],"ro--")
plt.plot([1,2,3,4], [2,4,6,8],"gs-.")
plt.legend(["2016年","2017年"],loc="best",title="图例的标题")
plt.title("2016-2017年各季度销售额对比图")

结果如下:
Here Insert Picture Description

③ ncol参数:图例显示的列数
plt.plot([1,3,5,7],[4,9,6,8],"ro--")
plt.plot([1,2,3,4], [2,4,6,8],"gs-.")
plt.legend(["2016年","2017年"],loc="best",ncol=2)

结果如下:
Here Insert Picture Description

④ frameon参数:设置是否显示图例的边框
plt.plot([1,3,5,7],[4,9,6,8],"ro--")
plt.plot([1,2,3,4], [2,4,6,8],"gs-.")
plt.legend(["2016年","2017年"],loc="best",ncol=2,frameon=False)

结果如下:
Here Insert Picture Description

4、网格设置

  可以通过plt的grid方法来设置是否显示网格。True为显示,False不显示。

  • 语法参数:ax.grid(color=, linestyle=, linewidth=)
  • color:设置网格线颜色。
  • axis:设置网格线显示x,y或者全部显示(x,y,both)。
  • linestyle:设置网格线形状。
  • linewidth: the width of the grid lines.

For example as follows:

plt.plot([1,3,5,7],[4,9,6,8],"ro--")
plt.grid(True,c="g",ls="-",lw=5)

The results are as follows:
Here Insert Picture Description

Published 79 original articles · won praise 95 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_41261833/article/details/104385319