Ich versuche, die Region, die dem Plot gewidmet ist, für ein bestimmtes xtick
Label (ohne Marker-Größe) zu bekommen. Zum Beispiel scheint label='b'
das Gebiet von (0.5 - 1.5)
b/c die Tick-Position ist 1
und ich glaube, die Standard-Tick-Breite ist 1
.Wie bekomme ich Position/Index von Xtick in Matplotlib mit Label? (Python 3.6 | Matplotlib 2.0)
Meine Frage, wenn ich ein query_label
(z query_label= "c"
) haben, wie kann ich die tick position
von query_label
entlang der x-axis
und die tick width
anzunehmen, jede tick
Position getrennt ist durch eine Breite von 1 (z [-1, 0, 1, 2, ...]
) bekommen?
Grundsätzlich würde Ich mag mit am Ende:
query_position = (1.5, 2.5) # for query_label = "c"
-Code für das einfache Beispiel:
# Data
x = np.arange(5)
y = np.sqrt(x)
s = [1000]*5
c = ["aquamarine"]*5
# Plot
with plt.style.context("dark_background"):
fig, ax = plt.subplots()
ax.scatter(x, y, c=c, s=s, edgecolors="ivory", linewidth=2.5)
ax.set_xticklabels(list("-abcde"), fontsize=20)
ax.grid(False, which="major")
# Trying to get the xtick info
ax.get_xticks()
# array([-1., 0., 1., 2., 3., 4., 5.])
# How to get position and tick width?
query_label= "c"
def get_position(query_label, ax):
# stuff to get tick index
# stuff to get tick width
tick_padding = tick_width/2
return (tick_index - tick_padding, tick_index + tick_padding)
# Results
query_position = get_position(query_label, ax)
query_position
# (1.5, 2.5)
Vielleicht gibt es ein Verfahren, bei matplotlib
analog zu pd.Index(list("-abcde")).get_loc("c")
?