Как я могу построить поверх существующего изображения с логарифмической осью? - PullRequest
1 голос
/ 18 октября 2019

У меня есть изображение, которое является контрольным графом с осью журнала. Я хочу разбросать свои данные поверх этого, но я не могу понять, как это сделать. Вот рабочий пример в matlab:

close all
figure
img = imread('psi_vratio.png');
imagesc([0.103 0.99],[0.512 0.8],flipud(img));
set(gca,'ydir','normal');
ax = gca;
ax.YTick = [0.5,0.6,0.7,0.8];
ax.XTick = [0.1,0.2,0.3,0.4,0.6,0.8,1.0];
set(ax,'XScale','log');
hold on
scatter(0.3,0.7);
ylabel('v_{ratio}');
xlabel('\phi');
print('logplot_outMatlab.png','-dpng');

enter image description here и мои попытки в python

 import matplotlib.pyplot as plt

fig = plt.figure()
im = plt.imread('psi_vratio.png')
plt.imshow(im, extent=[0.103, 0.99, 0.512, .8], aspect='auto')
plt.plot(0.3,0.7, 'rx')
plt.ylabel('$\phi$')
plt.ylabel('$V_{ratio}$')
plt.tight_layout()
plt.savefig('logplot_out0.png', dpi=300)

fig = plt.figure()
im = plt.imread('psi_vratio.png')
plt.xscale('log')
plt.imshow(im, extent=[0.103, 0.99, 0.512, .8], aspect='auto')
plt.plot(0.3,0.7, 'rx')
plt.ylabel('$\phi$')
plt.ylabel('$V_{ratio}$')
plt.tight_layout()
plt.savefig('logplot_out1.png', dpi=300)

enter image description here

Как сделать так, чтобы он не растянулся?

1 Ответ

0 голосов
/ 19 октября 2019

Как уже предлагалось в комментариях, лучший способ достичь желаемого результата - использовать две отдельные оси и сохранить исходное соотношение сторон:

import matplotlib.ticker as ticker

fig = plt.figure(figsize=(12,8))
im = plt.imread('psi_vratio.png')

#set first axes
ax = fig.add_subplot(1,1,1)
ax.imshow(im,  aspect='equal')
plt.axis('off')

#create second axes
newax = fig.add_axes(ax.get_position(), frameon=False)
newax.set_xlim((0.103, 0.99))
newax.set_ylim((0.512, .8))
newax.set_xlabel('$\phi$')
newax.set_ylabel('$V_{ratio}$')
newax.set_xscale('log')
#Change formatting of xticks
newax.xaxis.set_minor_formatter(ticker.FormatStrFormatter('%.1f'))
#plot point
newax.plot(0.3,0.7, 'rx')

это результат, который я получил: enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...