简体   繁体   English

在 Matplotlib 中设置区域颜色

[英]Setting color of area in Matplotlib

I'm creating a chart with matplotlib, here is my code:我正在使用 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)

And here is the output:这是 output:

The problem with the current output is that the colors of the two areas are "merging": basically the area BELOW the blue line should be blue, but instead it's green.当前 output 的问题是两个区域的 colors 正在“合并”:基本上蓝线下方的区域应该是蓝色的,但它是绿色的。 How can i set it to be blue, for example, like in my example?例如,我如何将其设置为蓝色,就像在我的示例中一样?

Example List data:示例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]]

I guess the hint of @JohanC is correct, you are plotting in the wrong order and overlay your previous plots with new ones.我猜@JohanC 的提示是正确的,您的绘图顺序错误,并用新的绘图覆盖了以前的绘图。

I tried to recreate a small example where total_buy1 > total_buy0 , so in order to get the desired result you first have to plot total_buy1 and then total_buy0 :我尝试重新创建一个小示例,其中total_buy1 > total_buy0 ,因此为了获得所需的结果,您首先必须 plot total_buy1然后total_buy0

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

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

n = 100
rate_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)

在此处输入图像描述

I noticed that you use Colors[Index][0] for both plotting calls, so the line and the area will not have different colors.我注意到您对两个绘图调用都使用了Colors[Index][0] ,因此线条和区域不会有不同的 colors。

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

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