我们在Python中经常使用会用到matplotlib画图,有些曲线和点的形状、颜色信息长时间不用就忘了,整理一下便于查找。
安装matplotlib后可以查看官方说明(太长不贴出来了)
1
2
|
from matplotlib import pyplot as plt help (plt.plot) |
常用颜色:
'b' 蓝色
'g' 绿色
'r' 红色
'c' 青色
'm' 品红
'y' 黄色
'k' 黑色
'w' 白色
更多颜色:
1
|
plt.plot(x, y, marker = '+' , color = 'coral' ) |
常用标记点形状:
‘.’:点(point marker)
‘,’:像素点(pixel marker)
‘o’:圆形(circle marker)
‘v’:朝下三角形(triangle_down marker)
‘^’:朝上三角形(triangle_up marker)
‘<‘:朝左三角形(triangle_left marker)
‘>’:朝右三角形(triangle_right marker)
‘1’:(tri_down marker)
‘2’:(tri_up marker)
‘3’:(tri_left marker)
‘4’:(tri_right marker)
‘s’:正方形(square marker)
‘p’:五边星(pentagon marker)
‘*’:星型(star marker)
‘h’:1号六角形(hexagon1 marker)
‘H’:2号六角形(hexagon2 marker)
‘+’:+号标记(plus marker)
‘x’:x号标记(x marker)
‘D’:菱形(diamond marker)
‘d’:小型菱形(thin_diamond marker)
‘|’:垂直线形(vline marker)
‘_’:水平线形(hline marker)
常用线形:
‘-‘:实线(solid line style)
‘–‘:虚线(dashed line style)
‘-.’:点划线(dash-dot line style)
‘:’:点线(dotted line style)
绘制散点图
在matplotlib中使用函数 matplotlib.pyplot.scatter 绘制散点图,matplotlib.pyplot.scatter的函数细节:
1
2
3
|
matplotlib.pyplot.scatter(x, y, s = None , c = None , marker = None , cmap = None , norm = None , vmin = None , vmax = None , alpha = None , linewidths = None , verts = None , edgecolors = None , hold = None , data = None , * * kwargs) x,y组成了散点的坐标;s为散点的面积;c为散点的颜色(默认为蓝色 'b' );marker为散点的标记;alpha为散点的透明度( 0 与 1 之间的数, 0 为完全透明, 1 为完全不透明);linewidths为散点边缘的线宽;如果marker为 None ,则使用verts的值构建散点标记;edgecolors为散点边缘颜色。 |
1
2
3
4
5
6
7
8
9
10
11
12
|
import matplotlib import matplotlib.pyplot as plt import numpy as np # 保证图片在浏览器内正常显示 % matplotlib inline # 10个点 N = 10 x = np.random.rand(N) y = np.random.rand(N) plt.scatter(x, y) plt.show() |
补充:Python散点图教程
调整散点大小
1
2
3
4
5
6
7
8
|
N = 10 x = np.random.rand(N) y = np.random.rand(N) area = np.random.rand(N) * 1000 # 包含10个均匀分布的随机值的面积数组,大小[0, 1000] fig = plt.figure() ax = plt.subplot() ax.scatter(x, y, s = area, alpha = 0.5 ) # 绘制散点图,面积随机 plt.show() |
调整散点颜色
1
2
3
4
5
6
7
8
9
10
11
|
N = 10 x = np.random.rand(N) y = np.random.rand(N) x2 = np.random.rand(N) y2 = np.random.rand(N) area = np.random.rand(N) * 1000 fig = plt.figure() ax = plt.subplot() ax.scatter(x, y, s = area, alpha = 0.5 ) ax.scatter(x2, y2, s = area, c = 'green' , alpha = 0.6 ) # 改变颜色 plt.show() |
调整散点形状
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
N = 10 x = np.random.rand(N) y = np.random.rand(N) x2 = np.random.rand(N) y2 = np.random.rand(N) x3 = np.random.rand(N) y3 = np.random.rand(N) area = np.random.rand(N) * 1000 fig = plt.figure() ax = plt.subplot() ax.scatter(x, y, s = area, alpha = 0.5 ) ax.scatter(x2, y2, s = area, c = 'green' , alpha = 0.6 ) ax.scatter(x3, y3, s = area, c = area, marker = 'v' , cmap = 'Reds' , alpha = 0.7 ) # 更换标记样式,另一种颜色的样式 plt.show() |
总结
到此这篇关于Python中Matplotlib的点、线形状、颜色以及绘制散点图的文章就介绍到这了,更多相关Python Matplotlib绘制散点图内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/hesongzefairy/article/details/113527780