如何在 matplotlib 中转换(或缩放)轴值并重新定义刻度频率?

发布于 2024-07-26 20:09:38 字数 922 浏览 2 评论 0原文

我正在显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞笑我太脆弱 2024-08-02 20:09:38

看起来您正在处理 matplotlib.pyplot 接口,这意味着您将能够绕过与艺术家、轴等相关的大部分处理。 您可以使用 控制刻度线的值和标签matplotlib.pyplot.xticks 命令,如下所示:

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) - 不过,你说过你知道如何做到这一点。

我没有太多处理图像,但您也许可以使用不同的绘图方法(例如 pcolor),该方法允许您提供 xy< /代码> 信息。 这可能会为您提供更多选项来指定图像的单位。

对于教程,您最好浏览 matplotlib gallery - 找到您喜欢的内容并阅读生成它的代码。 我们办公室的一个人最近买了一本关于 Python 可视化 的书 - 这可能是值得的看着。

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

  • Figure 是所有 Axes 的容器
  • Axes 是空间您绘制的内容(即您的绘图)实际显示的位置
  • Axis 是实际的 xy
  • 艺术家? 这对我来说在界面中太深了:尽管我很少在生产图中使用 pyplot 模块,但我从来不必担心这些。

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)

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 supply x and y 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:

  • 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文