Matplotlib:如何在一行中使用两种颜色?

Matplotlib: How to use two colors in a single line?

我得到了一个包含 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 值来为片段着色

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

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