简体   繁体   中英

How to set colors of specific bars in matplotlib object oriented way?

Currently there is no easy way to set specific colors for specific bars in matplotlib if you are using the object oriented method .

This leads to my second question which is what is the best way to plot in matplotlib (the pyplot one or the oop one)?

One hack I found:

  heights = x
  labels = y

  colors = ["dimgrey" for x in labels]
  if np.isin(true_val, labels):
    index = np.argmax(labels == true_val)
    colors[index] = "green"

  ax[3].bar(labels, heights, color=colors)

Pyplot way:

barlist=plt.bar([1,2,3,4], [1,2,3,4])
barlist[0].set_color('r')

Similar Questions:

Assign the the barcontainer returned from ax[1].bar(labels, heights, color="dimgrey") to a barlist and manipulate the colors using barlist .

heights = x
labels = y

barlist = ax[1].bar(labels, heights, color="dimgrey")

barlist[0].set_color('green')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM