如何转换(或缩放)轴值并在matplotlib中重新定义刻度频率? [英] How do I convert (or scale) axis values and redefine the tick frequency in matplotlib?

查看:150
本文介绍了如何转换(或缩放)轴值并在matplotlib中重新定义刻度频率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在显示jpg图像(如果相关,我会将其旋转90度),当然 轴显示像素坐标.我想转换轴,以便不显示像素数,而是显示我选择的单位-弧度,度数,或者在我的情况下是天文坐标.我知道从像素到(例如)度的转换.这是我的代码当前的摘要:

I am displaying a jpg image (I rotate this by 90 degrees, if this is relevant) and of course the axes display the pixel coordinates. I would like to convert the axis so that instead of displaying the pixel number, it will display my unit of choice - be it radians, degrees, or in my case an astronomical coordinate. I know the conversion from pixel to (eg) degree. Here is a snippet of what my code looks like currently:

import matplotlib.pyplot as plt 
import Image
import matplotlib
thumb = Image.open(self.image)
thumb = thumb.rotate(90)
dpi = plt.rcParams['figure.dpi']
figsize = thumb.size[0]/dpi, thumb.size[1]/dpi
fig = plt.figure(figsize=figsize)
plt.imshow(thumb, origin='lower',aspect='equal')
plt.show()

...因此,从此开始,我可以接受matplotlib将在轴上打印的每个值,并用字符串更改/替换它来输出吗?我想针对特定的坐标格式进行此操作-例如,我希望读取10 26'24''(即度,arcmins,arcsecs)而不是10.44(度)角

...so following on from this, can I take each value that matplotlib would print on the axis, and change/replace it with a string to output instead? I would want to do this for a specific coordinate format - eg, rather than an angle of 10.44 (degrees), I would like it to read 10 26' 24'' (ie, degrees, arcmins, arcsecs)

最后,关于这个主题,我想控制剧情中的滴答频率. Matplotlib可能每50像素打印一次轴值,但是我真的希望每度(例如)度数.

Finally on this theme, I'd want control over the tick frequency, on the plot. Matplotlib might print the axis value every 50 pixels, but I'd really want it every (for example) degree.

听起来我想用我想要显示的像素值及其转换后的值(度等)定义某种数组,并且可以控制xmin/xmax范围内的采样频率.

It sounds like I would like to define some kind of array with the pixel values and their converted values (degrees etc) that I want to be displayed, having control over the sampling frequency over the range xmin/xmax range.

在堆栈溢出方面有任何matplotlib专家吗?如果是这样,请非常感谢您的帮助!为了使这成为一次更多的学习体验,我非常感谢被引导进入有关此类matplotlib问题的教程等方向.我发现自己对轴,轴,人物,艺术家等感到非常困惑!

Are there any matplotlib experts on Stack Overflow? If so, thanks very much in advance for your help! To make this a more learning experience, I'd really appreciate being prodded in the direction of tutorials etc on this kind of matplotlib problem. I've found myself getting very confused with axes, axis, figures, artists etc!

干杯

戴夫

推荐答案

您似乎正在处理matplotlib.pyplot接口,这意味着您将能够绕过与艺术家,斧头,等等.您可以使用 matplotlib.pyplot.xticks来控制刻度线的值和标签. 命令,如下所示:

It looks like you're dealing with the matplotlib.pyplot interface, which means that you'll be able to bypass most of the dealing with artists, axes, and the like. You can control the values and labels of the tick marks by using the matplotlib.pyplot.xticks command, as follows:

tick_locs = [list of locations where you want your tick marks placed]
tick_lbls = [list of corresponding labels for each of the tick marks]
plt.xticks(tick_locs, tick_lbls)

对于您的特定示例,您必须计算相对于原始图的单位(即像素)的刻度线(因为您使用的是imshow)-您说过知道如何做到这一点但是.

For your particular example, you'll have to compute what the tick marks are relative to the units (i.e. pixels) of your original plot (since you're using imshow) - you said you know how to do this, though.

我对图像的处理不多,但是您可以使用其他绘图方法(例如pcolor)来提供xy信息.这样可能会为您提供更多用于指定图像单位的选项.

I haven't dealt with images much, but you may be able to use a different plotting method (e.g. pcolor) that allows you to supply x and y information. That may give you a few more options for specifying the units of your image.

对于教程,您最好浏览一下 matplotlib画廊-找到喜欢的东西,并阅读产生它的代码.我们办公室里的一个人最近在 Python可视化上买了一本书-可能值得看着.

For tutorials, you would do well to look through the matplotlib gallery - find something you like, and read the code that produced it. One of the guys in our office recently bought a book on Python visualization - that may be worthwhile looking at.

我通常认为所有各个方面的方式如下:

The way that I generally think of all the various pieces is as follows:

  • Figure是所有Axes
  • 的容器
  • Axes是您绘制的内容(即情节)实际显示的空间
  • Axis是实际的xy
  • 艺术家?对我来说,这太深了:即使我很少在生产区中使用pyplot模块,我也不必担心这些问题.
  • A Figure is a container for all the Axes
  • An Axes is the space where what you draw (i.e. your plot) actually shows up
  • An Axis is the actual x and y axes
  • Artists? That's too deep in the interface for me: I've never had to worry about those yet, even though I rarely use the pyplot module in production plots.

这篇关于如何转换(或缩放)轴值并在matplotlib中重新定义刻度频率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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