在 Matplotlib 中设置区域颜色

我正在使用 matplotlib 创建图表,这是我的代码:


fig = plt.figure(facecolor='#131722',dpi=155, figsize=(8, 4))

ax1 = plt.subplot2grid((1,2), (0,0), facecolor='#131722')


Colors = [['#0400ff', '#FF0000'], ['#09ff00', '#ff8c00']]


for x in List:


    Index = List.index(x)


    rate_buy = []

    total_buy = []


    for y in x['data']['bids']:

        rate_buy.append(y[0])

        total_buy.append(y[1])


    rBuys = pd.DataFrame({'buy': rate_buy})

    tBuys = pd.DataFrame({'total': total_buy})


    ax1.plot(rBuys.buy, tBuys.total, color=Colors[Index][0], linewidth=0.5, alpha=0.8)

    ax1.fill_between(rBuys.buy, 0, tBuys.total, facecolor=Colors[Index][0], alpha=1)

这是输出:

http://img4.mukewang.com/63aa55480001863805980526.jpg

当前输出的问题是两个区域的颜色正在“合并”:基本上蓝线下方的区域应该是蓝色的,但它是绿色的。我怎样才能将它设置为蓝色,例如,就像我的例子一样?

示例List数据:

[[9665, 0.07062500000000001], [9666, 0.943708], [9667, 5.683787000000001], [9668, 9.802289], [9669, 11.763305], [9670, 14.286004], [9671, 16.180122], [9672, 23.316723000000003], [9673, 30.915156000000003], [9674, 33.44226200000001], [9675, 36.14526200000001], [9676, 45.76024100000001], [9677, 51.85294700000001], [9678, 58.79529300000001], [9679, 59.05322900000001], [9680, 60.27704500000001], [9681, 60.743885000000006], [9682, 66.75103700000001], [9683, 71.86412600000001], [9684, 73.659636], [9685, 78.08502800000001], [9686, 78.19614200000001], [9687, 79.98396400000001], [9688, 90.55855800000002]]


青春有我
浏览 233回答 1
1回答

桃花长相依

您以错误的顺序绘制并用新的绘制覆盖了以前的绘制。我试图重新创建一个小示例,其中total_buy1> total_buy0,因此为了获得所需的结果,您首先必须绘制total_buy1 然后total_buy0:import matplotlib.pyplot as pltfig, ax = plt.subplots()Colors = [['#0400ff', '#FF0000'],          ['#09ff00', '#ff8c00']]n = 100rate_buy = np.linspace(0, 1000, 100)total_buy0 = np.linspace(0, 300, n)[::-1] + np.random.normal(scale=10, size=n)total_buy1 = np.linspace(0, 600, n)[::-1] + np.random.normal(scale=10, size=n)ax.plot(rate_buy, total_buy1, color=Colors[1][1], linewidth=0.5, alpha=0.8)ax.fill_between(rate_buy, 0, total_buy1, facecolor=Colors[1][0], alpha=1)ax.plot(rate_buy, total_buy0, color=Colors[0][1], linewidth=0.5, alpha=0.8)ax.fill_between(rate_buy, 0, total_buy0, facecolor=Colors[0][0], alpha=1)我注意到您Colors[Index][0]用于两个绘图调用,因此线条和区域不会有不同的颜色。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python