Bade
Bade

Reputation: 747

Matplotlib Barplot with Diverging Color Scheme

I am trying to make a horizontal barplot with just one bar. Here is sample code:

import matplotlib.pyplot as plt
plt.figure(figsize=(4, 1))
plt.barh(['my_value'], height =0.1, width=[-1,1], align='center')
plt.axvline(x=0.8, color='black')

enter image description here

How can I add a diverging color scheme/palette to it? Such that the color transitions from left to right smoothly. Solutions using Seaborn will work too.

Upvotes: 0

Views: 248

Answers (1)

JohanC
JohanC

Reputation: 80329

You could adapt the solution from the matplotlib tutorial example, provided you create only one bar (instead of two).

Or you could directly use imshow:

import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize=(4, 1))
plt.imshow(np.linspace(0, 1, 256).reshape(1, -1), extent=[-1, 1, -1, 1], aspect='auto', cmap='seismic')
plt.plot([-1, 1, 1, -1, -1], [-1, -1, 1, 1, -1], color='black')  # surrounding rectangle
plt.axvline(x=0.8, color='black', ls=':')
plt.xlim(-1.1, 1.1)
plt.ylim(-1.8, 1.8)
plt.yticks([0], ['my_value'])
plt.tight_layout()
plt.show()

gradient bar

Upvotes: 1

Related Questions