用于在球体上呈现数据的颜色方案

时间:2018-06-03 20:59:23

标签: python-2.7 matplotlib plot colors heatmap

嗨我有一个数据集,我投射到一个球体上,使用色谱(使用" ax.plot_surface&#34)显示数据的大小,作为θ和phi的函数。 ;," plt.colorbar"和#34; facecolors")。我的疑问是,在这个阶段,我被限制在" cm.hot"和" cm.jet"。有没有人知道任何其他可用于此目的的配色方案。请参阅我的代码和下面的数字

代码:

from numpy import*
import math
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.cm as cm

#theta inclination angle
#phi azimuthal angle
n_theta = 100 #number of values for theta
n_phi = 100  #number of values for phi
r = 1       #radius of sphere

theta, phi = np.mgrid[0: pi:n_theta*1j,-pi:pi:n_phi*1j ]

x = r*np.sin(theta)*np.cos(phi)
y = r*np.sin(theta)*np.sin(phi)
z = r*np.cos(theta)

inp = []
f = open("data.dat","r")
for line in f:
    i = float(line.split()[0])
    j = float(line.split()[1])
    val = float(line.split()[2])
    inp.append([i, j, val])

inp = np.array(inp)

#reshape the input array to the shape of the x,y,z arrays. 
c = inp[:,2].reshape((n_phi,n_theta))

#Set colours and render
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
#use facecolors argument, provide array of same shape as z
# cm.<cmapname>() allows to get rgba color from array.
# array must be normalized between 0 and 1
surf = ax.plot_surface(
    x,y,z,  rstride=1, cstride=1, facecolors=cm.jet(c), alpha=0.9, linewidth=1, shade=False) 
ax.set_xlim([-2.0,2.0])
ax.set_ylim([-2.0,2.0])
ax.set_zlim([-2,2])
ax.set_aspect("equal")

plt.title('Plot with cm.jet')

#Label axis. 
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

#Creates array for colorbar from 0 to 1. 
a = array( [1.0, 0.5, 0.0])

#Creates colorbar
m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(a)
plt.colorbar(m)
plt.savefig('facecolor plots')

f.close()
plt.show()

cm.hot cm.jet

1 个答案:

答案 0 :(得分:2)

以下是matplotlib直接提供的色彩映射列表。它来自Colormap reference example

cmaps = [('Perceptually Uniform Sequential', [
            'viridis', 'plasma', 'inferno', 'magma', 'cividis']),
         ('Sequential', [
            'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
            'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
            'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']),
         ('Sequential (2)', [
            'binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink',
            'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia',
            'hot', 'afmhot', 'gist_heat', 'copper']),
         ('Diverging', [
            'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu',
            'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']),
         ('Qualitative', [
            'Pastel1', 'Pastel2', 'Paired', 'Accent',
            'Dark2', 'Set1', 'Set2', 'Set3',
            'tab10', 'tab20', 'tab20b', 'tab20c']),
         ('Miscellaneous', [
            'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern',
            'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'hsv',
            'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar'])]

要轻松查看它们,例如使用以下3D色彩映射查看器(用PyQt5编写)。

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from PyQt5 import QtGui, QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import sys


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)

        self.main_widget = QtWidgets.QWidget(self)

        self.fig = Figure()
        self.canvas = FigureCanvas(self.fig)
        self.ax = self.fig.add_subplot(111, projection=Axes3D.name)
        u = np.linspace(0, 2 * np.pi, 100)
        v = np.linspace(0, np.pi, 100)
        x = 10 * np.outer(np.cos(u), np.sin(v))
        y = 10 * np.outer(np.sin(u), np.sin(v))
        z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))

        # Plot the surface
        self.surf = self.ax.plot_surface(x, y, z, cmap="YlGnBu")
        self.cb = self.fig.colorbar(self.surf)

        self.canvas.setSizePolicy(QtWidgets.QSizePolicy.Expanding, 
                                  QtWidgets.QSizePolicy.Expanding)
        self.canvas.updateGeometry()

        self.dropdown1 = QtWidgets.QComboBox()
        items = []
        for cats in cmaps:
            items.extend(cats[1])
        self.dropdown1.addItems(items)

        self.dropdown1.currentIndexChanged.connect(self.update)

        self.label = QtWidgets.QLabel("A plot:")

        self.layout = QtWidgets.QGridLayout(self.main_widget)
        self.layout.addWidget(QtWidgets.QLabel("Select Colormap"))
        self.layout.addWidget(self.dropdown1)
        self.layout.addWidget(self.canvas)

        self.setCentralWidget(self.main_widget)
        self.show()
        self.update()

    def update(self):
        self.surf.set_cmap(self.dropdown1.currentText())
        self.fig.canvas.draw_idle()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    win = MainWindow()
    sys.exit(app.exec_())

enter image description here