【问题标题】:Matplotlib plotting a single line that continuously changes colorMatplotlib 绘制一条连续改变颜色的线
【发布时间】:2019-08-29 15:19:06
【问题描述】:

我想在 (x,y) 平面上绘制一条曲线,其中曲线的颜色取决于另一个变量 T 的值。x 是一维 numpy 数组,y 是一维 numpy 数组。

T=np.linspace(0,1,np.size(x))**2
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)

我希望线根据 T 的值从蓝色变为红色(使用 RdBu 颜色图)(每个 (x,y) 对存在一个 T 值)。

我找到了这个,但我不知道如何将它转换为我的简单示例。我将如何在我的示例中使用 linecollection? http://matplotlib.org/examples/pylab_examples/multicolored_line.html

谢谢。

【问题讨论】:

  • 你试过什么?您对第二个示例情节有什么问题?请提出明确的问题,以便我们为您提供帮助。

标签: matplotlib


【解决方案1】:

一个想法可能是使用color=(R,G,B) 设置颜色,然后将您的绘图拆分为n 段,并不断改变R、G 或B(或组合)之一

import pylab as plt
import numpy as np

# Make some data
n=1000
x=np.linspace(0,100,n)
y=np.sin(x)

# Your coloring array
T=np.linspace(0,1,np.size(x))**2
fig = plt.figure()
ax = fig.add_subplot(111)

# Segment plot and color depending on T
s = 10 # Segment length
for i in range(0,n-s,s):
    ax.plot(x[i:i+s+1],y[i:i+s+1],color=(0.0,0.5,T[i]))

希望对你有帮助

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 2018-04-30
  • 2021-06-03
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
相关资源
最近更新 更多