我正在努力正确设置Seaborn图的字体大小。设置标题的字体大小可以很好地工作,但是不幸的是,关于轴和图例的字体大小,我还没有获得任何成功。这是我的代码(使用一些虚拟数据):
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
df = pd.DataFrame()
df['First PCA dimension'] = [1,2,3,4]
df['Second PCA dimension'] = [0,5,5,7]
df['Third PCA dimension'] = [1,2,6,4]
df['Data points'] = [1,2,3,4]
plt.figure(figsize=(42,30))
plt.title('2-D PCA of my data points',fontsize=32)
colors = ["#FF9926", "#2ACD37","#FF9926", "#FF0800"]
sns.scatterplot(x="First PCA dimension", y="Second PCA dimension", hue="Data points", palette=sns.color_palette(colors), data=df, legend="full", alpha=0.3)
sns.set_context("paper", rc={"font.size":48,"axes.titlesize":48,"axes.labelsize":48})
plt.savefig(outputPath+'/PCA/'+'PCA_2D.png',dpi=100,bbox_inches='tight')
,将导致以下输出:
在“ set_context”函数中更改数字没有任何作用。
答案 0 :(得分:2)
顺便说一句,图形尺寸太大(42x30)
:
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
df = pd.DataFrame()
df['First PCA dimension'] = [1,2,3,4]
df['Second PCA dimension'] = [0,5,5,7]
df['Third PCA dimension'] = [1,2,6,4]
df['Data points'] = [1,2,3,4]
plt.figure(figsize=(42,30))
plt.title('2-D PCA of my data points',fontsize=32)
colors = ["#FF9926", "#2ACD37","#FF9926", "#FF0800"]
b = sns.scatterplot(x="First PCA dimension", y="Second PCA dimension", hue="Data points", palette=sns.color_palette(colors), data=df, legend="full", alpha=0.3)
sns.set_context("paper", rc={"font.size":48,"axes.titlesize":48,"axes.labelsize":48})
b.set_ylabel('mylabely', size=54)
b.set_xlabel('mylabelx', size=54)
b.set_xticklabels([1,2,3,4,5,6,7,8], fontsize = 36)
lgnd = plt.legend(fontsize='22')
for handle in lgnd.legendHandles:
handle.set_sizes([26.0])
plt.show()