python画图渐变颜色的代号_带有行颜色渐变和颜色栏的python matplotlib

1586010002-jmsa.png

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()

Which gives me this:

6v86R.png

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.

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.

解决方案import numpy as np

import matplotlib.pyplot as plt

import matplotlib.collections as mcoll

def multicolored_lines():

"""

http://nbviewer.ipython.org/github/dpsanders/matplotlib-examples/blob/master/colorline.ipynb

http://matplotlib.org/examples/pylab_examples/multicolored_line.html

"""

x = np.linspace(0, 4. * np.pi, 100)

y = np.sin(x)

fig, ax = plt.subplots()

lc = colorline(x, y, cmap='hsv')

plt.colorbar(lc)

plt.xlim(x.min(), x.max())

plt.ylim(-1.0, 1.0)

plt.show()

def colorline(

x, y, z=None, cmap='copper', norm=plt.Normalize(0.0, 1.0),

linewidth=3, alpha=1.0):

"""

http://nbviewer.ipython.org/github/dpsanders/matplotlib-examples/blob/master/colorline.ipynb

http://matplotlib.org/examples/pylab_examples/multicolored_line.html

Plot a colored line with coordinates x and y

Optionally specify colors in the array z

Optionally specify a colormap, a norm function and a line width

"""

# Default colors equally spaced on [0,1]:

if z is None:

z = np.linspace(0.0, 1.0, len(x))

# Special case if a single number:

# to check for numerical input -- this is a hack

if not hasattr(z, "__iter__"):

z = np.array([z])

z = np.asarray(z)

segments = make_segments(x, y)

lc = mcoll.LineCollection(segments, array=z, cmap=cmap, norm=norm,

linewidth=linewidth, alpha=alpha)

ax = plt.gca()

ax.add_collection(lc)

return lc

def make_segments(x, y):

"""

Create list of line segments from x and y coordinates, in the correct format

for LineCollection: an array of the form numlines x (points per line) x 2 (x

and y) array

"""

points = np.array([x, y]).T.reshape(-1, 1, 2)

segments = np.concatenate([points[:-1], points[1:]], axis=1)

return segments

multicolored_lines()

RgCHG.png

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值