如何从 matplotlib/seaborn 图中删除或隐藏 y 轴刻度标签

我做了一个看起来像这样的情节

http://img.mukewang.com/64b5f5270001e18606230336.jpg

我想关闭 y 轴上的刻度标签。为此,我正在使用

plt.tick_params(labelleft=False, left=False)

现在剧情是这样的。即使标签被关闭,秤1e67仍然存在。

http://img3.mukewang.com/64b5f5350001133b05410304.jpg

关闭比例1e67会让情节看起来更好。我怎么做?



宝慕林4294392
浏览 428回答 1
1回答

精慕HU

seaborn用于绘制绘图,但它只是matplotlib.为删除 y 轴标签和刻度而调用的函数是matplotlib方法。创建绘图后,使用.set()..set(yticklabels=[])应该删除刻度标签。如果您使用,这不起作用.set_title(),但您可以使用.set(title='').set(ylabel=None)应删除轴标签。.tick_params(left=False)将去除蜱虫。同样,对于 x 轴:如何从 seaborn / matplotlib 图中删除或隐藏 x 轴标签?测试于python 3.11, pandas 1.5.2, matplotlib 3.6.2,seaborn 0.12.1实施例1import seaborn as snsimport matplotlib.pyplot as plt# load dataexercise = sns.load_dataset('exercise')pen = sns.load_dataset('penguins')# create figuresfig, ax = plt.subplots(2, 1, figsize=(8, 8))# plot datag1 = sns.boxplot(x='time', y='pulse', hue='kind', data=exercise, ax=ax[0])g2 = sns.boxplot(x='species', y='body_mass_g', hue='sex', data=pen, ax=ax[1])plt.show()删除标签fig, ax = plt.subplots(2, 1, figsize=(8, 8))g1 = sns.boxplot(x='time', y='pulse', hue='kind', data=exercise, ax=ax[0])g1.set(yticklabels=[])  # remove the tick labelsg1.set(title='Exercise: Pulse by Time for Exercise Type')  # add a titleg1.set(ylabel=None)  # remove the axis labelg2 = sns.boxplot(x='species', y='body_mass_g', hue='sex', data=pen, ax=ax[1])g2.set(yticklabels=[])  g2.set(title='Penguins: Body Mass by Species for Gender')g2.set(ylabel=None)  # remove the y-axis labelg2.tick_params(left=False)  # remove the ticksplt.tight_layout()plt.show()实施例2import numpy as npimport matplotlib.pyplot as pltimport pandas as pd# sinusoidal sample datasample_length = range(1, 1+1) # number of columns of frequenciesrads = np.arange(0, 2*np.pi, 0.01)data = np.array([(np.cos(t*rads)*10**67) + 3*10**67 for t in sample_length])df = pd.DataFrame(data.T, index=pd.Series(rads.tolist(), name='radians'), columns=[f'freq: {i}x' for i in sample_length])df.reset_index(inplace=True)# plotfig, ax = plt.subplots(figsize=(8, 8))ax.plot('radians', 'freq: 1x', data=df)# or skip the previous two lines and plot df directly# ax = df.plot(x='radians', y='freq: 1x', figsize=(8, 8), legend=False)删除标签# plotfig, ax = plt.subplots(figsize=(8, 8))ax.plot('radians', 'freq: 1x', data=df)# or skip the previous two lines and plot df directly# ax = df.plot(x='radians', y='freq: 1x', figsize=(8, 8), legend=False)ax.set(yticklabels=[])  # remove the tick labelsax.tick_params(left=False)  # remove the ticks
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python