更改seaborn图上轴的单位[重复]

问题描述 投票:0回答:1

我正在绘制一些距离函数,其中函数以特征距离振荡,并且我希望 x 轴的单位以该特征距离为单位。例如,如果特征距离为 200m 米,我希望 x 轴上的 x=50 为 0.25,x=100 为 0.5,依此类推。

这是我现在的代码:

import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

theta = 0.6
d_mass = 1
energy = 1

L_osc = (4*(np.pi)*energy)/d_mass

osc_constants = d_mass/(4*energy)

def P(x):
    return ((np.sin(2*theta))**2)*((np.sin(osc_constants*x))**2)

def S(x):
    return 1 - P(x)

x = np.linspace(0, 20, 1000)
y1 = P(x)
y2 = S(x)

df = pd.DataFrame(zip(x, y1, y2), columns=['x', 'Oscillation Probability', 'Survival      Probability']).set_index('x')
fig, ax = plt.subplots()

# Plot sns.lineplot() to the ax
sns.set_palette('Set2')
sns.set_style('ticks')
sns.lineplot(df, ax=ax)
ax.set_title('Plotting Functions in Matplotlib', size=14)
ax.set_xlim(0, 20)
ax.set_ylim(0, 1.5)

# Despine the graph
#sns.despine()
plt.show()`
python matplotlib seaborn graphing
1个回答
0
投票

我猜您正在寻找

plt.xticks()
函数(或者:
ax.set_xticks()
)。

在您的代码中,您可以添加以下行:

ax.set_xticks(ticks=np.linspace(0,20,5), labels=np.linspace(0,1,5) )

其中

ticks
对应于数据中的值,
labels
对应于您在轴上显示的值。

© www.soinside.com 2019 - 2024. All rights reserved.