简体   繁体   English

如何在 Pandas 中用两种颜色绘制单个图

[英]How to draw single plot with two colors in Pandas

I plotted below graph with the list of values.我在下面的图表中绘制了值列表。 But the graphs are not merging.但图表没有合并。 I would need the Red plot starts right after the blue plot.我需要在蓝色情节之后立即开始红色情节。

plt.plot(x_list[:251])
plt.plot(x_list[251:],color='r')

在此处输入图片说明

Create example data创建示例数据

x_list = np.random.randint(0,100,300)

Make another list, fill the data till 251 with NANs:制作另一个列表,用 NAN 填充数据直到 251:

y_list = [np.nan]*300
y_list[251:] = x_list[251:]

Plot阴谋

plt.plot(x_list[:251])
plt.plot(y_list,color='r')

Alternative data given by OP OP给出的替代数据

x_list = [[26756.0], [0.0], [831.0], [23676.0], [0.0], [0.0], [0.0], [1325.0], [33293.0], [32323.000000000004], [30535.0], [32351.0], [30859.000000000004], [28416.0]]

Note that I put brackets around np.nan to be in line with the input data - which is a nested list.请注意,我在np.nan周围加上括号以与输入数据保持一致 - 这是一个嵌套列表。 As the example list has length of 14, I used 10 as point to let start the second time-serie.由于示例列表的长度为 14,因此我使用 10 作为开始第二个时间序列的点。

y_list = [[np.nan]]*len(x_list)
y_list[10:] = x_list[10:]
plt.plot(x_list[:10])
plt.plot(y_list, color='r')

An alternative would be to flatten your nested list using list(np.array(x_list).flat) and then continue with the first solution.另一种方法是使用list(np.array(x_list).flat)展平嵌套列表,然后继续第一个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM