by Guest » 30 Dec 2024, 18:47
Ich versuche, Ticks aus meinem mit Seaborn gezeichneten Python-Plot zu extrahieren.
Ich habe zwei Codesätze unten, von denen ich dachte, dass sie zu denselben Ergebnissen führen würden. Allerdings extrahiert einer die Ticks korrekt und der andere gibt nur Nullen zurück.
Code: Select all
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca()
t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01)
ax.plot(t, s)
plt.show()
print([p.label.get_position()[0] for p in ax.xaxis.get_major_ticks()])
print([p.label.get_position()[0] for p in ax.xaxis.get_minor_ticks()])
print([p.label.get_position()[1] for p in ax.yaxis.get_major_ticks()])
print([p.label.get_position()[1] for p in ax.yaxis.get_minor_ticks()])
Ausgabe:
Code: Select all
[-20.0, 0.0, 20.0, 40.0, 60.0, 80.0, 100.0, 120.0]
[]
[-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0, 1.25]
[]
Der andere Teil des Codes ist mit Seaborn:
Code: Select all
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
fig = plt.figure()
ax = fig.gca()
t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01)
data = pd.DataFrame()
data['x'] = t
data['y'] = s
display(data)
sns_plot = sns.scatterplot(x='x', y='y', data=data, ax=ax)
sns_plot.set_title("test plot")
print([p.label.get_position()[0] for p in ax.xaxis.get_major_ticks()])
print([p.label.get_position()[0] for p in ax.xaxis.get_minor_ticks()])
print([p.label.get_position()[1] for p in ax.yaxis.get_major_ticks()])
print([p.label.get_position()[1] for p in ax.yaxis.get_minor_ticks()])
Ausgabe:
Code: Select all
[0, 0, 0, 0, 0, 0, 0, 0]
[]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[]
Kann mir jemand zeigen, wie ich die Tick-Beschriftungen aus meinem Seaborn-Plot extrahieren kann?
Oder kann mir jemand zeigen, was im zweiten Codeblock falsch ist?
Danke!
Ich versuche, Ticks aus meinem mit Seaborn gezeichneten Python-Plot zu extrahieren.
Ich habe zwei Codesätze unten, von denen ich dachte, dass sie zu denselben Ergebnissen führen würden. Allerdings extrahiert einer die Ticks korrekt und der andere gibt nur Nullen zurück.
[code]import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca()
t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01)
ax.plot(t, s)
plt.show()
print([p.label.get_position()[0] for p in ax.xaxis.get_major_ticks()])
print([p.label.get_position()[0] for p in ax.xaxis.get_minor_ticks()])
print([p.label.get_position()[1] for p in ax.yaxis.get_major_ticks()])
print([p.label.get_position()[1] for p in ax.yaxis.get_minor_ticks()])
[/code]
Ausgabe:
[code][-20.0, 0.0, 20.0, 40.0, 60.0, 80.0, 100.0, 120.0]
[]
[-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0, 1.25]
[]
[/code]
Der andere Teil des Codes ist mit Seaborn:
[code]import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
fig = plt.figure()
ax = fig.gca()
t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01)
data = pd.DataFrame()
data['x'] = t
data['y'] = s
display(data)
sns_plot = sns.scatterplot(x='x', y='y', data=data, ax=ax)
sns_plot.set_title("test plot")
print([p.label.get_position()[0] for p in ax.xaxis.get_major_ticks()])
print([p.label.get_position()[0] for p in ax.xaxis.get_minor_ticks()])
print([p.label.get_position()[1] for p in ax.yaxis.get_major_ticks()])
print([p.label.get_position()[1] for p in ax.yaxis.get_minor_ticks()])
[/code]
Ausgabe:
[code][0, 0, 0, 0, 0, 0, 0, 0]
[]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[]
[/code]
Kann mir jemand zeigen, wie ich die Tick-Beschriftungen aus meinem Seaborn-Plot extrahieren kann?
Oder kann mir jemand zeigen, was im zweiten Codeblock falsch ist?
Danke!