Asset Classes

Free investment financial education

Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
Python: Graph with Segmented Colors and Horizontal Multiple Graphs

Python: Graph with Segmented Colors and Horizontal Multiple Graphs

Posted January 17, 2023 at 3:54 pm

Sang-Heon Lee
SHLee AI Financial Model

This post shows how to plot a graph with segmented colors and then uses this to draw horizontal multiple graphs.

The following Python code in the Jupyter notebook plots a graph with segmented colors. It is done by splitting x and y respectively.

import numpy as np
import matplotlib.pyplot as plt
 
# sample data
x = np.arange(0.0, 2.0, 0.01)
y_fit  = np.sin(2*np.pi*x)
y_real = y_fit + (np.random.random(x.size)-0.5)*0.5
 
# graph with segmented colors 
fig, ax = plt.subplots()
 
ax.plot(   x[0:50], y_fit [0:50], color = 'blue')
ax.scatter(x[0:50], y_real[0:50], color = 'blue')
 
ax.plot(   x[50:100], y_fit [50:100], color = 'red')
ax.scatter(x[50:100], y_real[50:100], color = 'red')
 
ax.plot(   x[100:], y_fit [100:], color = 'black')
ax.scatter(x[100:], y_real[100:], color = 'black')
 
plt.show
 class=

Visit SH Fintech Modeling for additional insight on this topic: https://kiandlee.blogspot.com/2022/12/python-graph-with-segmented-colors-and.html.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from SHLee AI Financial Model and is being posted with its permission. The views expressed in this material are solely those of the author and/or SHLee AI Financial Model and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.