【问题标题】:Set background color to matplotlib chart将背景颜色设置为 matplotlib 图表
【发布时间】:2020-09-19 21:49:08
【问题描述】:

我正在使用 Matplotlib 生成一个订单簿图表,该图表已生成,但我很难弄清楚如何为其设置背景颜色。在图表上,我为每一方绘制了 2 个订单簿,为此我在我的数据上使用了一个简单的循环:

fig = plt.figure(facecolor='#131722',dpi=135, figsize=(5, 3))
ax1 = plt.subplot2grid((2,1), (0,0), rowspan=6, colspan=4, facecolor='#131722')


Colors = [['#2BBB2B', '#FF0000'], ['#09ff00', '#ff8c00']]

for x in List:

    Index = List.index(x)

    print(Index)

    rate_buy = []
    total_buy = []
    rate_sell = []
    total_sell = []

    for y in x['data']['asks']:
        rate_sell.append(y[0])
        total_sell.append(y[1])

    for y in x['data']['bids']:
        rate_buy.append(y[0])
        total_buy.append(y[1])

    rBuys = pd.DataFrame({'buy': rate_buy})
    rSells = pd.DataFrame({'sell': rate_sell})
    tBuys = pd.DataFrame({'total': total_buy})
    tSells = pd.DataFrame({'total': total_sell})

    plt.plot(rBuys.buy, tBuys.total, color=Colors[Index][0], linewidth=0.9, alpha=0.9)
    plt.plot(rSells.sell, tSells.total, color=Colors[Index][1],alpha=0.3, linewidth=0.9)

输出如下:

所以基本上,我想做的是将图表内部的区域设置为与值Color 相同的颜色。我该怎么做?

【问题讨论】:

    标签: python python-3.x matplotlib background area


    【解决方案1】:

    您可以使用np.vstack 函数为该区域创建一个包含xy 坐标的数组,然后通过Polygon 函数绘制它,如下所示:

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.patches import Polygon
    
    x = np.linspace(0, 10, 11)
    y1 = np.array([4, 5, 6, 9, 11, 7, 6, 2, 4, 4, 5])
    y2 = np.array([4, 3, 2, 1, 1, 6, 5, 7, 7, 6, 5])
    
    # here I concatenate the x and y arrays for the border of the area
    # two times x for back and forth
    # y1 and y2 for the top and bottom part of the area
    area = np.vstack((np.concatenate((x, x[::-1])),
                      np.concatenate((y1, y2[::-1])))).T
    
    fig, ax = plt.subplots(1, 1, figsize = (8, 8))
    
    ax.plot(x, y1, 'r-', lw = 2, label = 'y1')
    ax.plot(x, y2, 'b-', lw = 2, label = 'y2')
    
    # here I add the area to the plot
    ax.add_patch(Polygon(area, facecolor = 'g', alpha = 0.5))
    
    ax.grid()
    ax.legend()
    
    plt.show()
    

    给你这个:

    在你的图表中有 4 条曲线,我称之为浅绿色、深绿色、红色和棕色。 如果你想给图表的浅绿色和深绿色之间的部分着色,你应该按以下方式填写vstack向量:

    area_1 = np.vstack((np.concatenate((x_of_light-green, x_of_dark-green[::-1])),
                        np.concatenate((y_of_light-green, y_of_dark-green[::-1])))).T
    

    数组后面的[::-1] 是必要的,以便反转元素的顺序:使用x_of_light-greeny_of_light-green 定义第四个(彩色区域的底部边框),而使用@987654333 @ 和 y_of_dark-green[::-1] 定义背面(彩色区域的顶部边框)。

    而对于红色和棕色曲线之间的区域:

    area_2 = np.vstack((np.concatenate((x_of_red, x_of_brown[::-1])),
                        np.concatenate((y_of_red, y_of_brown[::-1])))).T
    

    x_of_...y_of_... 替换为您正在考虑的数据框的数据。
    一旦您定义了这些区域,您可以使用ax1.add_path(Polygon(area_1))ax1.add_path(Polygon(area_2)) 将它们添加到您的图表中。

    【讨论】:

    • 尝试将x_of_...y_of_... 替换为您用于图表的横坐标和纵坐标。另一种更简单的方法是使用方法ax.fill_between(x, y2, y1, facecolor = 'green', alpha = 0.5)
    猜你喜欢
    • 2021-09-30
    • 2023-03-25
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 2022-11-30
    相关资源
    最近更新 更多