0

我在 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')
4

1 回答 1

0

swarmplot是一个分类图。它在 x 轴上绘制类别。使用plt.xticks([2,3,4,5,6,7,8])您为类别 2 到 8 设置刻度,但您的绘图中有更多类别。

对于使用中的数据,它看起来不像分类图实际上很有用。您可能更愿意使用散点图,它是两个维度的数字图。

您可以使用ax.scatter()orsns.scatterplot()代替。

于 2018-08-07T21:45:38.897 回答