带有行颜色渐变和颜色栏的python matplotlib [英] python matplotlib with a line color gradient and colorbar

查看:470
本文介绍了带有行颜色渐变和颜色栏的python matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在解决这个问题,并且很接近我想要的东西,但是又少了一两行.

I've been toying around with this problem and am close to what I want but missing that extra line or two.

基本上,我想绘制一条线,该线的颜色在给定第三个数组的值的情况下会发生变化.潜伏着我发现这很好用(尽管很慢)并且代表了问题

Basically, I'd like to plot a single line whose color changes given the value of a third array. Lurking around I have found this works well (albeit pretty slowly) and represents the problem

import numpy as np
import matplotlib.pyplot as plt
c = np.arange(1,100)
x = np.arange(1,100)
y = np.arange(1,100)

cm = plt.get_cmap('hsv')

fig = plt.figure(figsize=(5,5))
ax1 = plt.subplot(111)

no_points = len(c)
ax1.set_color_cycle([cm(1.*i/(no_points-1)) 
                     for i in range(no_points-1)])

for i in range(no_points-1):
    bar = ax1.plot(x[i:i+2],y[i:i+2])
plt.show()

哪个给我这个:

我希望能够在此绘图中包含一个颜色栏.到目前为止,我还无法破解.可能会有其他的行包含不同的x,y,但相同的c,因此我一直认为Normalize对象是正确的路径.

I'd like to be able to include a colorbar along with this plot. So far I haven't been able to crack it just yet. Potentially there will be other lines included with different x,y's but the same c, so I was thinking that a Normalize object would be the right path.

更大的图景是该图是2x2子图网格的一部分.我已经使用matplotlib.colorbar.make_axes(ax4)来为颜色条轴对象腾出空间,其中ax4具有第4个子图.

Bigger picture is that this plot is part of a 2x2 sub plot grid. I am already making space for the color bar axes object with matplotlib.colorbar.make_axes(ax4), where ax4 with the 4th subplot.

推荐答案

看看 multicolored_line Matplotlib图库

请注意,多次调用plt.plot会降低性能. 使用LineCollection构建多色线段要快得多.

Note that calling plt.plot hundreds of times tends to kill performance. Using a LineCollection to build multi-colored line segments is much much faster.

这篇关于带有行颜色渐变和颜色栏的python matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆