Python 如何在seaborn的facetgrid中设置可读的xticks?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43727278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set readable xticks in seaborn's facetgrid?
提问by jll
i have this plot of a dataframe with 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 plots all the xtick labels instead of just picking a few and it looks horrible:
seaborn 绘制了所有的 xtick 标签,而不是只挑选几个,看起来很可怕:
Is there a way to customize it so that it plots every n-th tick on x-axis instead of all of them?
有没有办法自定义它,以便它在 x 轴上绘制每个第 n 个刻度而不是所有刻度?
采纳答案by mwaskom
The seaborn.pointplot
is not the right tool for this plot. But the answer is very simple: use the basic matplotlib.pyplot.plot
function:
的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])
回答by Serenity
You have to skip x labels manually like in this example:
您必须手动跳过 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()