Python matplotlib 颜色函数

标签 python matplotlib colors

我想使用类似于 Mathematica 中的 ColorFunction对于我在 python 中的绘图。

换句话说,我想调用pyplot.plot(x, y, color=c),其中c是一个向量,定义了每个元素的颜色数据点。

有没有什么方法可以使用 matplotlib 库实现这一点?

最佳答案

据我所知,Matplotlib 中没有等效项,但我们可以通过以下两个步骤得到类似的结果:绘制不同颜色的点和绘制线。

这是一个演示。

enter image description here


源代码,

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
import random

fig, ax = plt.subplots()

nrof_points = 100 
x = np.linspace(0, 10, nrof_points)
y = np.sin(x)
colors = cm.rainbow(np.linspace(0, 1, nrof_points))     # generate a bunch of colors

# draw points
for idx, point in enumerate(zip(x, y)):
    ax.plot(point[0], point[1], 'o', color=colors[idx], markersize=10)

# draw the line
ax.plot(x, y, 'k')
plt.grid()

plt.show()

关于Python matplotlib 颜色函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37926746/

相关文章:

java - 动态彩色碧 Jade

python - 使用 Python 逐行处理非常大的 900M 行 MySQL 表

python - 将标签对齐在一个圆圈中

java - 使用 JFrame 创建颜色网格?

java - 无法改变背景 Swing 的颜色

python - matplotlib fatal error ft2

python - 给定通用维度的开始和结束索引,对 NumPy 数组进行切片

python - Windows 错误 [5] : Access Denied H2o Deep Learning Framework Initialization using Python

python - swig 构建问题 - python

python - PyQt4 matplotlib 不绘制我的实时数据