如何在具有对数轴的现有图像上绘制图? [英] How do I plot on top of an existing image with a logarithmic axis?

查看:44
本文介绍了如何在具有对数轴的现有图像上绘制图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像,它是带有对数轴的参考图.我想将我的数据分散在它上面,但我不知道该怎么做.这是matlab中的一个工作示例:

I have an image that is a reference graph with a log axis. I want to scatter my data on top of it, but I cannot figure out how to do it. Here is a working example in 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 中的尝试

and my attemps in 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)

我怎样才能做到这点呢?

How can I make it so it is not all stretched out?

推荐答案

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

As suggested already in the comments, the best way to achieve the result you want is to use two separate axes and keep the original aspect ratio:

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

这是我得到的结果:

这篇关于如何在具有对数轴的现有图像上绘制图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆