1

I'm generating an orderbook chart using Matplotlib, the chart is generated but i'm having an hard time figuring how to set a background color to it. On the chart, i'm plotting 2 orderbooks for every side, to do this i'm using a simple loop on my data:

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) 

The output is the following: enter image description here

So basically, what i want to do, is set the area INSIDE the chart with the same color of the value Color. How can i do that?

1 Answer 1

1

You can use a np.vstack function to create an array of x and y coordinates for the area, then plot it trhough the Polygon function, as this:

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

which gives you this:

enter image description here

In your chart there are 4 curves, which I call light-green, dark-green, red and brown. If you want to color the part of the graph between light-green and dark-green you should fill in the vstack vector in the following way:

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 

The [::-1] after the arrays is necessary in order to reverse the order of the element: with the x_of_light-green and y_of_light-green you define the forth (the bottom border of the colored area), while with the x_of_dark-green[::-1] and y_of_dark-green[::-1] you define the back (the top border of the colored area).

While for the region between the red and brown curves:

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

Replace x_of_... and y_of_... with the data of the dataframe you are considering.
Once you have defined these area, you can add them to your graph with the ax1.add_path(Polygon(area_1)) and ax1.add_path(Polygon(area_2)).

Sign up to request clarification or add additional context in comments.

1 Comment

Try to replace the x_of_... and y_of_... with the abscissas and ordinates you used for you graph. Another, simpler way is to use the method ax.fill_between(x, y2, y1, facecolor = 'green', alpha = 0.5)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.