0

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.

Bade
  • 747
  • 3
  • 12
  • 28

1 Answers1

1

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

JohanC
  • 71,591
  • 8
  • 33
  • 66