我在将swarmplot和Pointplot中的xticks设置为seaborn时遇到了一些问题(尽管使用传统的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')
答案 0 :(得分:0)
swarmplot
是分类图。它在x轴上绘制类别。使用plt.xticks([2,3,4,5,6,7,8])
可以为类别2到8设置刻度线,但是您的绘图中还有更多类别。
对于正在使用的数据,实际上看起来没有很多使用分类图。您可能宁愿使用散点图,这是两个维度上的数字图。