Python 如何使用对数轴在现有图像的顶部打印?

Python 如何使用对数轴在现有图像的顶部打印?,python,matplotlib,Python,Matplotlib,我有一个图像,它是一个带有对数轴的参考图。我想把我的数据分散在上面,但我不知道怎么做。以下是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.

我有一个图像,它是一个带有对数轴的参考图。我想把我的数据分散在上面,但我不知道怎么做。以下是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');
还有我在python中的尝试

导入matplotlib.pyplot作为plt
图=plt.图()
im=plt.imread('psi_vratio.png')
plt.imshow(im,区段=[0.103,0.99,0.512,8],aspect='auto')
plt.图(0.3,0.7,‘rx’)
plt.ylabel(“$\phi$”)
plt.ylabel(“$V{ratio}$”)
plt.紧_布局()
plt.savefig('logplot\u out0.png',dpi=300)
图=plt.图()
im=plt.imread('psi_vratio.png')
plt.xscale('log')
plt.imshow(im,区段=[0.103,0.99,0.512,8],aspect='auto')
plt.图(0.3,0.7,‘rx’)
plt.ylabel(“$\phi$”)
plt.ylabel(“$V{ratio}$”)
plt.紧_布局()
plt.savefig('logplot\u out1.png',dpi=300)


如何使其不完全伸展?

如评论中所述,实现所需结果的最佳方法是使用两个单独的轴并保持原始纵横比:

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')

这就是我得到的结果:

正如评论中已经建议的那样,实现所需结果的最佳方法是使用两个单独的轴并保持原始纵横比:

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')

这是我得到的结果:

大概需要两个轴。背景中的一个是线性的,显示图像,前景中的另一个具有与背景轴相同的限制,并具有对数缩放。原始图像需要
aspect='equal'
,这样图像不会拉伸,然后需要调整对数轴的大小,使其与图像轴的大小相同。可能需要两个轴。一个在背景中是线性的,显示图像,另一个在前景中,与背景轴具有相同的限制,并且具有对数缩放。原始图像需要
aspect='equal'
,因此图像不会拉伸,然后需要调整对数轴的大小,使其与图像轴的大小相同。太好了,谢谢!这正是我正在寻找的。我很高兴它帮助了你,如果我的答案解决了你的问题,请考虑点击检查马克。太好了,谢谢!这正是我正在寻找的。我很高兴它帮助了你,如果我的答案已经解决了你的问题,请考虑点击复选标记。