在 matplotlib 中同时更改线宽和颜色 [英] Changing the linewidth and the color simultaneously in matplotlib

查看:74
本文介绍了在 matplotlib 中同时更改线宽和颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上图是一幅出色的艺术品,同时显示了风速,风向和温度.详细:

  • X 轴代表日期
  • Y轴显示风向(南,西等)
  • 线的变宽代表整个时间序列中的风速
  • 线条的变体代表大气温度

这个简单的图形可视化了 3 个不同的属性,没有冗余.

所以,我真的很想在 matplotlib 中重现类似的情节.

我现在的尝试

  ##参考1 http://stackoverflow.com/questions/19390895/matplotlib-plot-with-variable-line-width##参考2 http://stackoverflow.com/questions/17240694/python-how-to-plot-one-line-in-different-colorsdef plot_colourline(x,y,c):c = plt.cm.jet((c-np.min(c))/(np.max(c)-np.min(c)))lwidths=1+x[:-1]ax = plt.gca()对于我在 np.arange(len(x)-1):ax.plot([x[i],x[i+1]], [y[i],y[i+1]], c=c[i],linewidth = lwidths[i])# = lwidths[一世])返回x=np.linspace(0,4*math.pi,100)y = np.cos(x)lwidths=1+x[:-1]fig = plt.figure(1, figsize=(5,5))ax = fig.add_subplot(111)plot_colourline(x,y,prop)ax.set_xlim(0,4 * math.pi)ax.set_ylim(-1.1,1.1)

有人有更感兴趣的方法来实现这一目标吗?任何建议将不胜感激!

解决方案

作为灵感使用

函数 windline 接受带有 x、y、偏差(如每个 x 值的厚度值)和用于颜色映射的颜色数组的 numpy 数组作为参数.我认为可以通过乱七八糟的其他细节来大大改善它,但原理虽然不完美,但应该是扎实的.

The figure above is a great artwork showing the wind speed, wind direction and temperature simultaneously. detailedly:

  • The X axes represent the date
  • The Y axes shows the wind direction(Southern, western, etc)
  • The variant widths of the line were stand for the wind speed through timeseries
  • The variant colors of the line were stand for the atmospheric temperature

This simple figure visualized 3 different attribute without redundancy.

So, I really want to reproduce similar plot in matplotlib.

My attempt now

## Reference 1 http://stackoverflow.com/questions/19390895/matplotlib-plot-with-variable-line-width
## Reference 2 http://stackoverflow.com/questions/17240694/python-how-to-plot-one-line-in-different-colors

def plot_colourline(x,y,c):
    c = plt.cm.jet((c-np.min(c))/(np.max(c)-np.min(c)))
    lwidths=1+x[:-1]
    ax = plt.gca()
    for i in np.arange(len(x)-1):
        ax.plot([x[i],x[i+1]], [y[i],y[i+1]], c=c[i],linewidth = lwidths[i])# = lwidths[i])
    return

x=np.linspace(0,4*math.pi,100)
y=np.cos(x)
lwidths=1+x[:-1]

fig = plt.figure(1, figsize=(5,5))
ax  = fig.add_subplot(111)
plot_colourline(x,y,prop)

ax.set_xlim(0,4*math.pi)
ax.set_ylim(-1.1,1.1)

Does someone has a more interested way to achieve this? Any advice would be appreciate!

解决方案

Using as inspiration another question.

One option would be to use fill_between. But perhaps not in the way it was intended. Instead of using it to create your line, use it to mask everything that is not the line. Under it you can have a pcolormesh or contourf (for example) to map color any way you want.

Look, for instance, at this example:

import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d

def windline(x,y,deviation,color):
    y1 = y-deviation/2
    y2 = y+deviation/2
    tol = (y2.max()-y1.min())*0.05
    X, Y = np.meshgrid(np.linspace(x.min(), x.max(), 100), np.linspace(y1.min()-tol, y2.max()+tol, 100))
    Z = X.copy()
    for i in range(Z.shape[0]):
        Z[i,:] = c

    #plt.pcolormesh(X, Y, Z)
    plt.contourf(X, Y, Z, cmap='seismic')

    plt.fill_between(x, y2, y2=np.ones(x.shape)*(y2.max()+tol), color='w')
    plt.fill_between(x, np.ones(x.shape) * (y1.min() - tol), y2=y1, color='w')
    plt.xlim(x.min(), x.max())
    plt.ylim(y1.min()-tol, y2.max()+tol)
    plt.show()

x = np.arange(100)
yo = np.random.randint(20, 60, 21)
y = interp1d(np.arange(0, 101, 5), yo, kind='cubic')(x)
dv = np.random.randint(2, 10, 21)
d = interp1d(np.arange(0, 101, 5), dv, kind='cubic')(x)
co = np.random.randint(20, 60, 21)
c = interp1d(np.arange(0, 101, 5), co, kind='cubic')(x)
windline(x, y, d, c)

, which results in this:

The function windline accepts as arguments numpy arrays with x, y , a deviation (like a thickness value per x value), and color array for color mapping. I think it can be greatly improved by messing around with other details but the principle, although not perfect, should be solid.

这篇关于在 matplotlib 中同时更改线宽和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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