Specified color looks different when saved as pdf

Viewed 394

I am plotting something with a hatched area. However, when saving it as a .pdf the color is different from what I specified it. In the console it is displayed correctly and also when saving as a .png file it shows the correct color. I am wondering if this has been observed before and if someone knows a workaround / fix for this or if I am doing something wrong.

This is what I want the picture to look like:

enter image description here

This is what it looks like in the pdf:

enter image description here

As you can see the hatching works fine for all peaks except the red one. In the pdf the hatching is gray instead of red. Also the colors in the pdf seem to be more transparent than in the png. I am using matplotlib version 3.2.2. For reproduction here is my code:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from matplotlib.ticker import MultipleLocator

def gauss(x,mu,sigma,a):
    return a*np.exp(-(x-mu)**2/(2*sigma**2))

x = np.linspace(0,10,1000)


data1 = gauss(x,6.5,0.5,10)
data2 = gauss(x,3,0.8,15)
data3 = gauss(x,5,0.2,12)
data4 = gauss(x,8,0.4,11)
data = data1 + data2 + data3 + data4

fs = 20
ls=20
plt.figure()
ax = plt.gca()

plt.plot(x,data,'k.',mfc='none',label='Data')
plt.plot(x,data,'r')
plt.fill(x,data1,'gold',ec='darkgoldenrod',label='1')
plt.fill(x,data2,'crimson',ec='darkred',hatch='//',alpha=0.5,label='2')
plt.fill(x,data3,'mediumslateblue',ec='b',hatch='\\\\' , alpha=0.5,label='3')
plt.fill(x,data4,'limegreen',ec='darkgreen',hatch='++',alpha=0.5,label='4')
plt.xlabel('x',fontsize=fs)
plt.ylabel('y',fontsize=fs)
ax.tick_params(axis='both', which='major', width=1.5, length=5, direction='in', pad=15)
ax.tick_params(axis='both', which='minor', width=1., length=3.5, direction='in', pad=15)

handles, labels= ax.get_legend_handles_labels()
handles[0], handles[1], handles[2], handles[3], handles[4] =  handles[1], handles[2],handles[3], handles[4],handles[0]
labels[0], labels[1], labels[2], labels[3], labels[4] = labels[1], labels[2], labels[3], labels[4], labels[0]
ax.legend(handles,labels,loc='upper left',fontsize=15)

ax.yaxis.set_minor_locator(MultipleLocator(1))
ax.yaxis.set_major_locator(MultipleLocator(5))
tmp = list(ax.get_yticklabels())
ax.set_yticklabels(['' for _ in tmp])
ax.xaxis.set_minor_locator(MultipleLocator(1))
ax.xaxis.set_major_locator(MultipleLocator(5))
plt.xlim(10,0)
plt.tick_params(labelsize=ls)
plt.tight_layout()
plt.savefig('Correct_colors.png')
plt.savefig('Incorrect_colors.pdf')

Any help is highly appreciated.

0 Answers
Related