How to add constant ticks on an axis whose lengths vary? [Python]

To simplify my problem (this is not entirely true, but I prefer simple answers to simple questions):

I have several 2D maps that display areas of rectangular areas. I would like to add maps and ticks on the axis to show the distances on this map (with matplotlib, since the old code is with it), but the problem is that the areas have different sizes. I would like to put nice, clear ticks on the axis, but the width and height of the cards can be anything ...

To try to explain what I mean: let's say I have a map of the region, the size of which is 4.37 km * 6.42 km. I want the axes along the axes at 0, 1, 2, 3 and 4 km: s and along the axes along the O axis there are 0, 1, 2, 3, 4, 5 and 6 km: s. However, the image and axes reach a little more than 4 km and 6 km, since the area is more than 4 km * 6 km.

The space between ticks can be constant, 1 km. However, the sizes of the maps vary greatly (say, between 5-15 km), and they are swimming values. My current script knows the size of the area and can scale the image in the correct ratio of height and width, but how to say where to put the labels?

Perhaps a solution to the problem already exists, but since I could not find suitable search words for my problem, I should have asked about it here ...

+5
source share
3 answers

Just set the tick locator to use matplotlib.ticker.MultipleLocator(x), where xis the interval 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 of this is that no matter how we scale or interact with the schedule, it will always be ticked at a distance of 1 unit. enter image description here

+5
source

This should give you ticks in all integer values ​​within the current axis on the x axis:

from matplotlib import pylab as plt
import math

# get values for the axis limits (unless you already have them)
xmin,xmax = plt.xlim()

# get the outermost integer values using floor and ceiling 
# (I need to convert them to int to avoid a DeprecationWarning),
# then get all the integer values between them using range
new_xticks = range(int(math.ceil(xmin)),int(math.floor(xmax)+1))
plt.xticks(new_xticks,new_xticks)
# passing the same argment twice here because the first gives the tick locations
# and the second gives the tick labels, which should just be the numbers

Repeat for y axis.

Out of curiosity: what tics do you get by default?

0
source

, , , , , PDF , ( ) . , , , !

, , . .

STEP_PART. STEP_PART, ( , ). , STEP_PART 5, 1 /5 = 200 , 200 .

STEP_PART = 5             # In the start of the program.

height = 6.42             # These are actually given elsewhere,
width = 4.37              # but just as example...

vHeight = range(0, int(STEP_PART*height), 1)  # Make tick vectors, now in format
                                              # 0, 1, 2... instead of 0, 0.2...
vWidth = range(0, int(STEP_PART*width), 1)    # Should be divided by STEP_PART 
                                              # later to get right values.

(0, 1, 2... , 0, 0.2, 0.4... ), km "". km STEP_PART, .

for j in range(len(vHeight)):
    if (j % STEP_PART != 0):
        vHeight[j] = ""
    else:
        vHeight[j] = int(vHeight[j]/STEP_PART)

for i in range(len(vWidth)):
    if (i % STEP_PART != 0):
        vWidth[i] = ""
    else:
        vWidth[i] = int(vWidth[i]/STEP_PART)

, , ( x ). x - , shape() ( , ... , , ).

xt = np.linspace(0,x-1,len(vWidth)+1) # For locating those ticks on the same distances.
locs, labels = mpl.xticks(xt, vWidth, fontsize=9) 

Repeat for y axis. The result is a graph in which marks are marked for every 200 m, but the data marks are in integer km values. In any case, the accuracy of these axes is 200 m, this is not accurate, but that was enough for me. The script will be even better if I learn how to grow the size of whole ticks ...

0
source

All Articles