#185 Lollipop plot with conditional color





This page explain how to have several colors on the same lollipop plot, and how to make this color depends the Y value. Here the color is blue if the value is under 0, orange if not. This helps to make the chart clearer!







# libraries
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

# Data
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x) + np.random.uniform(size=len(x)) - 0.2

# Create a color if the group is "B"
my_color=np.where(y>=0, 'orange', 'skyblue')

# The vertival plot is made using the hline function
# I load the seaborn library only to benefit the nice looking feature
import seaborn as sns
plt.vlines(x=x, ymin=0, ymax=y, color=my_color, alpha=0.4)
plt.scatter(x, y, color=my_color, s=1, alpha=1)

# Add title and axis names
plt.title("Evolution of the value of ...", loc='left')
plt.xlabel('Value of the variable')

Leave a Reply

Your email address will not be published.