Python Matplotlib/Seaborn shareX为具有字符串x轴值的所有子地块创建了错误的x标签
我正在使用for循环创建子地块图,当我打开“sharex”时,所有子地块的x标签都不正确,并且与最后一个子地块匹配。我希望它为每个可用标签创建一个勾号Python Matplotlib/Seaborn shareX为具有字符串x轴值的所有子地块创建了错误的x标签,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我正在使用for循环创建子地块图,当我打开“sharex”时,所有子地块的x标签都不正确,并且与最后一个子地块匹配。我希望它为每个可用标签创建一个勾号 import pandas as pd import matplotlib.pyplot as plt import seaborn as sns df = pd.DataFrame({ 'letters' : ['A','A','A', 'B','B','B', 'C','C','C', 'D','D','D'], 'v
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame({
'letters' : ['A','A','A', 'B','B','B', 'C','C','C', 'D','D','D'],
'values' : [1, 2, 3, 3, 2, 1, 2, 3, 1, 2, 3, 4],
'options': ['O 1', 'O 2', 'O 3', 'O 3', 'O 2', 'O 1',
'O 2','O 3','O 1','O 2','O 3','O 4'],
})
list_letters = ['A', 'B', 'C', 'D']
fig,axes = plt.subplots(nrows = 1, ncols = len(list_letters), sharey = True, sharex = True)
for letter in list_letters:
index = list_letters.index(letter)
df2 = df[(df['letters'] == letter)]
sns.scatterplot(x="options", y="values",data = df2,
hue = 'options', ax=axes[index], legend = False)
axes[index].set_title(letter, rotation = 60, verticalalignment = 'bottom', ha = 'left')
我专门制作了数据帧,因此选项O1对于所有“字母”的值都为1。对于O2、O3和O4也是如此
关闭shareX时(错误):打开shareX时(True):
请注意,这些值如何与选项不正确对应,只有3个标签,并且所有子批次都与禁用shareX时的最后一个子批次顺序相匹配 这是字符串x轴的问题。如果我将“选项”列更改为数值,则一切正常。请参见此处:
有没有办法通过for循环创建子批次,打开ShareX,并将四个标签O1、O2、O3、O4和所有值正确对齐?不幸的是,目前无法预先确定轴的单位。这使得绘制多个分类数据集变得很麻烦。
一个棘手的解决办法是按照预期的顺序将某些东西绘制到轴上,然后将其删除。这是通过下面代码中的
prepare_axes
功能完成的
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'letters' : ['A','A','A', 'B','B','B', 'C','C','C', 'D','D','D'],
'values' : [1, 2, 3, 3, 2, 1, 2, 3, 1, 2, 3, 4],
'options': ['O 1', 'O 2', 'O 3', 'O 3', 'O 2', 'O 1',
'O 2','O 3','O 1','O 2','O 3','O 4'],
})
list_letters = ['A', 'B', 'C', 'D']
def prepare_axes(x, ax):
ax.autoscale(False)
line, = ax.plot(x, np.zeros(len(x)))
line.remove()
ax.relim()
ax.autoscale(True)
# determine all needed categoricals
uniqueoptions = df["options"].unique()
fig,axes = plt.subplots(nrows = 1, ncols = len(list_letters), sharey = True, sharex = True)
for letter in list_letters:
index = list_letters.index(letter)
df2 = df[(df['letters'] == letter)]
_, hue = np.unique(df2["options"].values, return_inverse=True)
prepare_axes(uniqueoptions, axes[index])
axes[index].scatter(x="options", y="values", c=hue, data = df2)
axes[index].set_title(letter, rotation = 60, verticalalignment = 'bottom', ha = 'left')
plt.show()