Nil*_*ils 6 python colors matplotlib
我正在绘制带有阴影区域的东西。但是,将其保存为 .pdf 时,颜色与我指定的颜色不同。在控制台中它显示正确,并且在另存为 .png 文件时它显示正确的颜色。我想知道这之前是否已经观察到,是否有人知道解决方法/修复此问题,或者我是否做错了什么。
这就是我希望图片的样子:
这是它在pdf中的样子:
如您所见,除红色峰外,阴影线适用于所有峰。在 pdf 中,阴影线是灰色而不是红色。此外,pdf 中的颜色似乎比 png 中的更透明。我正在使用 matplotlib 版本 3.2.2。复制这里是我的代码:
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')
Run Code Online (Sandbox Code Playgroud)
任何帮助都受到高度赞赏。
| 归档时间: |
|
| 查看次数: |
156 次 |
| 最近记录: |