如何在 matplotlib 中转换(或缩放)轴值并重新定义刻度频率?
我正在显示 jpg 图像(如果相关的话,我将其旋转 90 度),当然 轴显示像素坐标。 我想转换轴,以便它不显示像素数,而是显示我选择的单位 - 无论是弧度、度数,还是在我的情况下是天文坐标。 我知道从像素到(例如)度的转换。 这是我的代码当前的一个片段:
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.44(度)的角度,我希望它读取 10 26' 24'' (即度、arcmins、arcsecs)
最后关于这个主题,我想控制绘图上的滴答频率。 Matplotlib 可能每 50 像素打印一次轴值,但我真的希望每(例如)度打印一次。
听起来我想用我想要显示的像素值及其转换值(度等)定义某种数组,并控制 xmin/xmax 范围内的采样频率。
Stack Overflow 上有 matplotlib 专家吗? 如果是这样,非常感谢您的帮助! 为了让这成为一种更多的学习体验,我真的很感激有人在此类 matplotlib 问题上提供教程等方面的指导。 我发现自己对轴、轴、图形、艺术家等感到非常困惑!
干杯,
戴夫
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()
...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)
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.
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.
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!
Cheers,
Dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在处理 matplotlib.pyplot 接口,这意味着您将能够绕过与艺术家、轴等相关的大部分处理。 您可以使用
控制刻度线的值和标签matplotlib.pyplot.xticks
命令,如下所示:对于您的特定示例,您必须计算刻度线相对于原始绘图的单位(即像素)的值(因为您'正在使用
imshow
) - 不过,你说过你知道如何做到这一点。我没有太多处理图像,但您也许可以使用不同的绘图方法(例如
pcolor
),该方法允许您提供x
和y< /代码> 信息。 这可能会为您提供更多选项来指定图像的单位。
对于教程,您最好浏览 matplotlib gallery - 找到您喜欢的内容并阅读生成它的代码。 我们办公室的一个人最近买了一本关于 Python 可视化 的书 - 这可能是值得的看着。
我通常认为所有各个部分的方式如下:
Figure
是所有Axes
的容器Axes
是空间您绘制的内容(即您的绘图)实际显示的位置Axis
是实际的x
和y
轴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 thematplotlib.pyplot.xticks
command, as follows: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.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 supplyx
andy
information. That may give you a few more options for specifying the units of your image.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
is a container for all theAxes
Axes
is the space where what you draw (i.e. your plot) actually shows upAxis
is the actualx
andy
axespyplot
module in production plots.