1

I have the following MWE

dat = [1,7,3,6,9,4]
plt.bar(range(len(dat)), dat, color='#1C5B26')
plt.savefig(path/tofile.pdf)

When I run this, then inspect the pdf, the hex value of the bars can be very far from what I specified. I would like the plot to be exactly the color I have specified, but I can even visually see that it is not close the the given hex value.

Any reasons this is happening/workarounds?

EDIT: Here is what I get when inspect. For the given hex value above I should get (28,91,38). I know its a small difference, but I thought the problem was worse when I was using a different "display in ..." option. sRGB is the option that I found gave me the best match, but still not perfect. enter image description here

Nate Stemen
  • 1,235
  • 3
  • 15
  • 27
  • I can't reproduce this issue. I created a figure using your code, then extracted the color from a pdf and it was exactly the same as what I specified in the script. Perhaps your pdf viewer has a different color profile set? Maybe you could show an example of your output to illustrate the issue. – tmdavison Aug 14 '17 at 15:41
  • @tom I was inspecting with a different option that led me to believe the difference was much larger, but even with sRGB there is still a small, albeit very small, difference. – Nate Stemen Aug 14 '17 at 16:48

1 Answers1

1

I would want to comment this but I can't (<50 rep).

Going by the first answer from Named colors in matplotlib matplotlib is using a dict for it's colours.
He also links every available one. Using this dictionary for colors is the reason why you can't really choose whatever color you want.

  • Why would they not allow for arbitrary hex code? and do you know of any workarounds? I don't really want to search for the one thats the closest. – Nate Stemen Aug 14 '17 at 14:36
  • That answer only shows which named colors are available. Any RGB color can be specified (for example using a hex code), even if it doesn't have a name in that dict – tmdavison Aug 14 '17 at 15:36