Seaborn lineplot 在 xticks 范围内的意外行为
Seaborn lineplot unexpected behaviour in the range of xticks
我开始学习 pandas 和 seaborn。我正在测试线图,但该图的 x 轴未显示我对该属性的预期范围 (num_of_elements
)。我希望此属性的每个值都显示在 x 轴上。有人可以解释我在这个情节中缺少什么吗?谢谢
这是我正在使用的代码:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import ticker
df_scability = pd.DataFrame()
df_scability['num_of_elements'] = [13,28,43,58,73,88,93,108,123,138]
df_scability['time_minutes'] = [2,3,5,7,20,30,40,50,60,90]
df_scability['dataset'] = ['Top 10 users','Top 10 users','Top 10 users','Top 10 users','Top 10 users','Top 10 users',
'Top 10 users','Top 10 users','Top 10 users','Top 10 users']
dpi = 600
fig = plt.figure(figsize=(3, 2),dpi=dpi)
ax = sns.lineplot(x = "num_of_elements", y = "time_minutes", hue='dataset', err_style='bars', data = df_scability)
ax.legend(loc='upper left', fontsize=4)
sns.despine(offset=0, trim=True, left=True)
ax.yaxis.set_major_locator(ticker.MultipleLocator(10))
ax.set_yticklabels(ax.get_ymajorticklabels(), fontsize = 6)
ax.yaxis.set_major_formatter(ticker.ScalarFormatter())
ax.set_xticklabels(ax.get_xmajorticklabels(), fontsize = 6)
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())
plt.ylabel('AVG time (min)',fontsize=7)
plt.xlabel('Number of elements',fontsize=7)
plt.tight_layout()
plt.show()
此代码生成它作为我的输出:
行:
sns.despine(offset=0, trim=True, left=True)
从情节中删除刺,所以它可能会造成混乱。 x 轴实际上是从 6.75 到 144.25:
print(ax.get_xlim())
# (6.75, 144.25)
但只显示 50 和 100 个值的刻度。
所以你可以固定 x 轴范围:
ax.set_xticks(range(0, 150 + 50, 50))
在调用 despine
之前。 0
是最低报价,150
是最高报价,50
是报价中的阶梯。您可以根据需要定制它们。
完整代码
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import ticker
df_scability = pd.DataFrame()
df_scability['num_of_elements'] = [13,28,43,58,73,88,93,108,123,138]
df_scability['time_minutes'] = [2,3,5,7,20,30,40,50,60,90]
df_scability['dataset'] = ['Top 10 users','Top 10 users','Top 10 users','Top 10 users','Top 10 users','Top 10 users',
'Top 10 users','Top 10 users','Top 10 users','Top 10 users']
dpi = 600
fig = plt.figure(figsize=(3, 2),dpi=dpi)
ax = sns.lineplot(x = "num_of_elements", y = "time_minutes", hue='dataset', err_style='bars', data = df_scability)
ax.legend(loc='upper left', fontsize=4)
ax.set_xticks(range(0, 150 + 50, 50))
sns.despine(offset=0, trim=True, left=True)
ax.yaxis.set_major_locator(ticker.MultipleLocator(10))
ax.set_yticklabels(ax.get_ymajorticklabels(), fontsize = 6)
ax.yaxis.set_major_formatter(ticker.ScalarFormatter())
ax.set_xticklabels(ax.get_xmajorticklabels(), fontsize = 6)
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())
plt.ylabel('AVG time (min)',fontsize=7)
plt.xlabel('Number of elements',fontsize=7)
plt.tight_layout()
plt.show()
我开始学习 pandas 和 seaborn。我正在测试线图,但该图的 x 轴未显示我对该属性的预期范围 (num_of_elements
)。我希望此属性的每个值都显示在 x 轴上。有人可以解释我在这个情节中缺少什么吗?谢谢
这是我正在使用的代码:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import ticker
df_scability = pd.DataFrame()
df_scability['num_of_elements'] = [13,28,43,58,73,88,93,108,123,138]
df_scability['time_minutes'] = [2,3,5,7,20,30,40,50,60,90]
df_scability['dataset'] = ['Top 10 users','Top 10 users','Top 10 users','Top 10 users','Top 10 users','Top 10 users',
'Top 10 users','Top 10 users','Top 10 users','Top 10 users']
dpi = 600
fig = plt.figure(figsize=(3, 2),dpi=dpi)
ax = sns.lineplot(x = "num_of_elements", y = "time_minutes", hue='dataset', err_style='bars', data = df_scability)
ax.legend(loc='upper left', fontsize=4)
sns.despine(offset=0, trim=True, left=True)
ax.yaxis.set_major_locator(ticker.MultipleLocator(10))
ax.set_yticklabels(ax.get_ymajorticklabels(), fontsize = 6)
ax.yaxis.set_major_formatter(ticker.ScalarFormatter())
ax.set_xticklabels(ax.get_xmajorticklabels(), fontsize = 6)
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())
plt.ylabel('AVG time (min)',fontsize=7)
plt.xlabel('Number of elements',fontsize=7)
plt.tight_layout()
plt.show()
此代码生成它作为我的输出:
行:
sns.despine(offset=0, trim=True, left=True)
从情节中删除刺,所以它可能会造成混乱。 x 轴实际上是从 6.75 到 144.25:
print(ax.get_xlim())
# (6.75, 144.25)
但只显示 50 和 100 个值的刻度。
所以你可以固定 x 轴范围:
ax.set_xticks(range(0, 150 + 50, 50))
在调用 despine
之前。 0
是最低报价,150
是最高报价,50
是报价中的阶梯。您可以根据需要定制它们。
完整代码
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import ticker
df_scability = pd.DataFrame()
df_scability['num_of_elements'] = [13,28,43,58,73,88,93,108,123,138]
df_scability['time_minutes'] = [2,3,5,7,20,30,40,50,60,90]
df_scability['dataset'] = ['Top 10 users','Top 10 users','Top 10 users','Top 10 users','Top 10 users','Top 10 users',
'Top 10 users','Top 10 users','Top 10 users','Top 10 users']
dpi = 600
fig = plt.figure(figsize=(3, 2),dpi=dpi)
ax = sns.lineplot(x = "num_of_elements", y = "time_minutes", hue='dataset', err_style='bars', data = df_scability)
ax.legend(loc='upper left', fontsize=4)
ax.set_xticks(range(0, 150 + 50, 50))
sns.despine(offset=0, trim=True, left=True)
ax.yaxis.set_major_locator(ticker.MultipleLocator(10))
ax.set_yticklabels(ax.get_ymajorticklabels(), fontsize = 6)
ax.yaxis.set_major_formatter(ticker.ScalarFormatter())
ax.set_xticklabels(ax.get_xmajorticklabels(), fontsize = 6)
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())
plt.ylabel('AVG time (min)',fontsize=7)
plt.xlabel('Number of elements',fontsize=7)
plt.tight_layout()
plt.show()