欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python画3D散点图并添加colorbar

程序员文章站 2022-03-27 12:55:27
Python画3D散点图,自定义colormap并添加色带...
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.colors as colors
import matplotlib.cm as cmx

def main():
	x = [1,1.3,1.5,2,3]
	y = [1,1.2,1.5,2,3]
	z = [1,1.5,1.5,2,3]
	values = [-0.5,0.2, 0.4,0.5, 1]

	colorslist = ['green','yellow','red']
	cmaps = colors.LinearSegmentedColormap.from_list('mylist', colorslist, N = 800)

	fig = plt.figure()
	ax = fig.add_subplot(111, projection='3d')
	cNorm = colors.Normalize(vmin=min(values), vmax=max(values))
	scatter = ax.scatter(x, y, z, c = values, cmap=cmaps, marker="D", s=2,
		norm=cNorm)

	scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cmaps)
	scalarMap.set_array(values)
	fig.colorbar(scalarMap)

	ax.set_xlabel("x")
	ax.set_ylabel("y")
	ax.set_zlabel("z")

	plt.show()


if __name__ == '__main__':
	main()

Python画3D散点图并添加colorbar

本文地址:https://blog.csdn.net/qq_25102303/article/details/107652821

相关标签: Python