如何在 seaborn 的 facetgrid 中设置可读的 xticks?
how to set readable xticks in seaborn's facetgrid?
我有这个带有 seaborn 的 facetgrid 的数据框图:
import seaborn as sns
import matplotlib.pylab as plt
import pandas
import numpy as np
plt.figure()
df = pandas.DataFrame({"a": map(str, np.arange(1001, 1001 + 30)),
"l": ["A"] * 15 + ["B"] * 15,
"v": np.random.rand(30)})
g = sns.FacetGrid(row="l", data=df)
g.map(sns.pointplot, "a", "v")
plt.show()
seaborn 绘制了所有 xtick 标签,而不是只挑选了几个标签,它看起来很糟糕:
有没有办法对其进行自定义,使其在 x 轴上绘制每个第 n 个刻度而不是所有刻度?
您必须像本例中那样手动跳过 x 个标签:
import seaborn as sns
import matplotlib.pylab as plt
import pandas
import numpy as np
df = pandas.DataFrame({"a": range(1001, 1031),
"l": ["A",] * 15 + ["B",] * 15,
"v": np.random.rand(30)})
g = sns.FacetGrid(row="l", data=df)
g.map(sns.pointplot, "a", "v")
# iterate over axes of FacetGrid
for ax in g.axes.flat:
labels = ax.get_xticklabels() # get x labels
for i,l in enumerate(labels):
if(i%2 == 0): labels[i] = '' # skip even labels
ax.set_xticklabels(labels, rotation=30) # set new labels
plt.show()
seaborn.pointplot
不是该图的正确工具。但答案很简单:使用基本的 matplotlib.pyplot.plot
函数:
import seaborn as sns
import matplotlib.pylab as plt
import pandas
import numpy as np
df = pandas.DataFrame({"a": np.arange(1001, 1001 + 30),
"l": ["A"] * 15 + ["B"] * 15,
"v": np.random.rand(30)})
g = sns.FacetGrid(row="l", data=df)
g.map(plt.plot, "a", "v", marker="o")
g.set(xticks=df.a[2::8])
我有这个带有 seaborn 的 facetgrid 的数据框图:
import seaborn as sns
import matplotlib.pylab as plt
import pandas
import numpy as np
plt.figure()
df = pandas.DataFrame({"a": map(str, np.arange(1001, 1001 + 30)),
"l": ["A"] * 15 + ["B"] * 15,
"v": np.random.rand(30)})
g = sns.FacetGrid(row="l", data=df)
g.map(sns.pointplot, "a", "v")
plt.show()
seaborn 绘制了所有 xtick 标签,而不是只挑选了几个标签,它看起来很糟糕:
有没有办法对其进行自定义,使其在 x 轴上绘制每个第 n 个刻度而不是所有刻度?
您必须像本例中那样手动跳过 x 个标签:
import seaborn as sns
import matplotlib.pylab as plt
import pandas
import numpy as np
df = pandas.DataFrame({"a": range(1001, 1031),
"l": ["A",] * 15 + ["B",] * 15,
"v": np.random.rand(30)})
g = sns.FacetGrid(row="l", data=df)
g.map(sns.pointplot, "a", "v")
# iterate over axes of FacetGrid
for ax in g.axes.flat:
labels = ax.get_xticklabels() # get x labels
for i,l in enumerate(labels):
if(i%2 == 0): labels[i] = '' # skip even labels
ax.set_xticklabels(labels, rotation=30) # set new labels
plt.show()
seaborn.pointplot
不是该图的正确工具。但答案很简单:使用基本的 matplotlib.pyplot.plot
函数:
import seaborn as sns
import matplotlib.pylab as plt
import pandas
import numpy as np
df = pandas.DataFrame({"a": np.arange(1001, 1001 + 30),
"l": ["A"] * 15 + ["B"] * 15,
"v": np.random.rand(30)})
g = sns.FacetGrid(row="l", data=df)
g.map(plt.plot, "a", "v", marker="o")
g.set(xticks=df.a[2::8])