【问题标题】:Matplotlib: How to use two colors in a single line?Matplotlib:如何在一行中使用两种颜色?
【发布时间】:2021-12-07 00:31:03
【问题描述】:

我得到了一份close 数据列表,它既是消极的又是积极的。当我绘制一条线时,我想将正值显示为绿色段,将负值显示为红色段。我有以下 df 格式的数据:

                                A       price     B         side  size  signal  \
time                                                                            
2019-06-12 03:54:26.668990  4603.35936  7990.0  4583.96620  Buy    20    True   
2019-06-12 03:54:26.668990  4603.24884  7990.0  4583.96620  Buy    38    True   
2019-06-12 03:54:26.668990  4603.26808  7990.0  4583.96620  Buy    69    True   
2019-06-12 03:54:26.668990  4603.32670  7990.0  4583.96620  Buy    25    True   
2019-06-12 03:54:26.668990  4603.32670  7990.0  4583.96620  Buy   450    True   
...                                ...     ...         ...  ...   ...     ...   
2019-06-12 12:07:48.793863  3997.85136  8043.5  4375.44562  Buy    22   False   
2019-06-12 12:07:48.793863  3997.87648  8044.0  4375.44562  Buy  1300   False   
2019-06-12 12:07:48.793863  3997.87616  8044.0  4375.44562  Buy     6   False   
2019-06-12 12:07:48.793863  3997.89530  8044.0  4375.44562  Buy  1000   False   
2019-06-12 12:07:48.793863  3997.90046  8044.0  4375.44562  Buy   280   False

如果信号为真,则显示绿色,否则显示红色。我找到了this 示例,但我很难理解它。

到目前为止我尝试过的代码如下

first=combine[:200000] #DF
x = first.index
y = first.price.values
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
from  matplotlib.colors import LinearSegmentedColormap
cmap=LinearSegmentedColormap.from_list('rg',["r", "g"], N=256) 
print(cmap)

我无法弄清楚如何使用Signal 值来为段着色

【问题讨论】:

    标签: python matplotlib colormap


    【解决方案1】:

    如果要在折线图中以数据段为单位更改线条颜色,则需要将用于折线图的数据转换为 (x1,y1),(x2,y2)。然后创建一个列表来设置该部分的颜色。对于每个数据,指定绘图函数; x 轴上的时间序列将最后更新。可能可以按原样处理时间序列,但我认为将其作为向量处理,然后再将其作为时间序列处理会更容易。这是这个答案的提示。我从this answer 修改的是指定最后一个值的方式,因为使用的数据是熊猫系列。

    import pandas as pd
    import numpy as np
    import io
    
    data = '''
    time A       price     B         side  size  signal 
    "2019-06-12 03:54:26.668990"  4603.35936  7990.0  4583.96620  Buy    20    True   
    "2019-06-12 03:54:26.668990"  4603.24884  7990.0  4583.96620  Buy    38    True   
    "2019-06-12 03:54:26.668990"  4603.26808  7990.0  4583.96620  Buy    69    True   
    "2019-06-12 03:54:26.668990"  4603.32670  7990.0  4583.96620  Buy    25    True   
    "2019-06-12 03:54:26.668990"  4603.32670  7990.0  4583.96620  Buy   450    True   
    "2019-06-12 12:07:48.793863"  3997.85136  8043.5  4375.44562  Buy    22   False   
    "2019-06-12 12:07:48.793863"  3997.87648  8044.0  4375.44562  Buy  1300   False   
    "2019-06-12 12:07:48.793863"  3997.87616  8044.0  4375.44562  Buy     6   False   
    "2019-06-12 12:07:48.793863"  3997.89530  8044.0  4375.44562  Buy  1000   False   
    "2019-06-12 12:07:48.793863"  3997.90046  8044.0  4375.44562  Buy   280   False
    '''
    
    df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
    
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    fig, ax = plt.subplots()
    
    y = df['price']
    x = np.arange(len(y))
    
    # x:numpy.array, y:pandas.Series
    segments_x = np.r_[x[0], x[1:-1].repeat(2), x[-1]].reshape(-1, 2)
    segments_y = np.r_[y[0], y[1:-1].repeat(2), y[:-1]].reshape(-1, 2)
    # print(segments_x, segments_y, sep='\n')
    colors = ['green' if x == True else 'red' for x in df['signal']]
    segments = [[x_, y_] for x_, y_ in zip(segments_x, segments_y)]
    # print(segments)
    
    for s,c in zip(segments, colors):
        ax.plot(s[0],s[1],color=c)
    
    ax.set_xticks(x)
    ax.set_xticklabels(df['time'].tolist(), rotation=90)
    
    plt.show()
    

    【讨论】:

    • 颜色判断是根据信号列的布尔值创建颜色列表。
    • 出色的答案,简单、合乎逻辑且切中要害!
    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 2013-03-18
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 2018-12-04
    • 2021-07-08
    相关资源
    最近更新 更多