如何在长度不同的轴上添加恒定间隔的刻度? [蟒蛇] [英] How to add constant-spaced ticks on axes whose lenghts vary? [Python]

查看:187
本文介绍了如何在长度不同的轴上添加恒定间隔的刻度? [蟒蛇]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了简化我的问题(它不完全是这样的,但我更喜欢简单的问题的简单答案):

我有几张描绘矩形区域区域的2D地图。我想添加地图坐标轴和刻度以显示此地图上的距离(使用matplotlib,因为旧的代码与它一起),但问题是这些区域大小不同。我想把斧头放在很好的,清晰的蜱虫上,但是地图的宽度和高度可以是任何东西...... 试图解释我的意思:假设我有一张地图,其大小为4.37公里* 6.42公里。我希望在0,1,2,3和4 km上有x轴刻度,在0,1,2,3,4,5和6 km上有y轴刻度:s。然而,由于该地区大于4公里* 6公里,因此图像和坐标轴进一步延伸至4公里和6公里以外。

蜱可以是恒定的,1公里。但是,地图的大小差异很大(比如,在5-15公里之间),它们是浮点值。我当前的脚本知道区域的大小,并可以将图像缩放到正确的高度/宽度比例,但如何告诉它在哪里放置滴答滴答?

可能有已经解决了这个问题,但由于我找不到适合我的问题的搜索词,所以我不得不在这里提问...

>

只需设置标记定位器使用 matplotlib.ticker.MultipleLocator(x)其中 x 是间距你想要的(例如上面例子中的1.0)。

 导入numpy的作为NP 
进口matplotlib.pyplot作为PLT
。从matplotlib.ticker进口MultipleLocator,FormatStrFormatter

x = np.arange(20)
y = x * 0.1

fig,ax = plt.subplots()
ax.plot(x, Y)

ax.xaxis.set_major_locator(MultipleLocator(1.0))
ax.yaxis.set_major_locator(MultipleLocator(1.0))

#强制积为标有 普通 的整数,而不是科学记数法
ax.xaxis.set_major_formatter(FormatStrFormatter( '%I'))

plt.show()

这样做的好处是,无论我们如何缩放或与绘图交互,它总会被标记为相隔1单位的刻度。


To simplify my problem (it's not exactly like that but I prefer simple answers to simple questions):

I have several 2D maps that portray rectangular region areas. I'd like to add on the map axes and ticks to show the distances on this map (with matplotlib, since the old code is with it), but the problem is that the areas are different sized. I'd like to put on the axes nice, clear ticks, but the widths and heights of the maps can be anything...

To try to explain what I mean: Let's say I have a map of a region whose size is 4.37 km * 6.42 km. I want that there is on x-axis ticks on 0, 1, 2, 3, and 4 km:s and on y-axis ticks on 0, 1, 2, 3, 4, 5, and 6 km:s. However, the image and the axes reach a bit further than to 4 km and 6 km, since the region is larger then 4 km * 6 km.

The space between the ticks can be constant, 1 km. However, the sizes of the maps vary quite a lot (let's say, between 5-15 km), and they are float values. My current script knows the size of the region and can scale the image into right height/width ratio, but how to tell it where to put the ticks?

There may be already solution for this problem, but since I couldn't find suitable search words for my problem, I had to ask it here...

解决方案

Just set the tick locator to use matplotlib.ticker.MultipleLocator(x) where x is the spacing that you want (e.g. 1.0 in your example above).

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

x = np.arange(20)
y = x * 0.1

fig, ax = plt.subplots()
ax.plot(x, y)

ax.xaxis.set_major_locator(MultipleLocator(1.0))
ax.yaxis.set_major_locator(MultipleLocator(1.0))

# Forcing the plot to be labeled with "plain" integers instead of scientific notation
ax.xaxis.set_major_formatter(FormatStrFormatter('%i'))

plt.show()

The advantage to this is that no matter how we zoom or interact with the plot, it will always be labeled with ticks 1 unit apart.

这篇关于如何在长度不同的轴上添加恒定间隔的刻度? [蟒蛇]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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