我在 seaborn 的 swarmplot 和 pointplot 中设置 xticks 时遇到了一些问题(尽管使用传统的 plt.plot 没问题)。当我使用典型的命令时:
plt.xticks([2,3,4,5,6,7,8])
或者:
ax2.xaxis.set_major_locator(ticker.LinearLocator(8))
xticks 仅在不覆盖整个 x 轴的短范围内。 https://imgur.com/c3MxEfv
(图像上的左子图)。当我不输入 plt.xticks(... 时,我会在正确的子图上得到类似的东西。
我的代码在这里:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
df = pd.read_csv('dane/iris.csv')
df['Legend'] = df['variety']
df['variety'].replace( {'Setosa':1, 'Versicolor':2, 'Virginica':3}, inplace = True)
fig = plt.figure(figsize = (13,7), facecolor = 'white')
ax1 = fig.add_subplot(1,2,1)
sns.swarmplot(df['sepal.length'], df['sepal.width'], hue = df['Legend'], ax = ax1)
plt.xlabel('Sepal length', fontsize = 14)
plt.ylabel('Sepal width', fontsize = 14)
plt.xticks([])
plt.xticks([2,3,4,5,6,7,8])
ax2 = fig.add_subplot(122)
sns.swarmplot(df['petal.length'], df['petal.width'], hue = df['Legend'], ax = ax2)
plt.xlabel('Petal length', fontsize = 14)
plt.ylabel('Petal width', fontsize = 14)
plt.savefig(Figure = fig, fname = 'iris.png')