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

时间:2020-09-05 17:20:17

标签: python matplotlib data-visualization seaborn

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

enter image description here

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

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

现在情节看起来像这样。即使标签已关闭,刻度1e67仍然保留。 enter image description here

关闭比例1e67将使绘图看起来更好。我该怎么办?

1 个答案:

答案 0 :(得分:0)

  • serializedData = [ json.dumps(user) for user in allUsers ] 用于绘制图,但这只是seaborn的高级API。
    • 用于删除y轴标签和刻度线的函数是matplotlib方法。
  • 创建图解后,使用matplotlib
  • .set()应该删除刻度线标签。
    • 如果您使用.set(yticklabels=[]),则此方法不起作用,但是您可以使用.set_title()
  • .set(title='')应该删除轴标签。
  • .set(ylabel=None)将删除刻度线。
  • 类似地,对于x轴:How to remove or hide x-axis labels from a seaborn / matplotlib plot?

示例1

.tick_params(left=False)

enter image description here

删除标签

import seaborn as sns
import matplotlib.pyplot as plt

# load data
exercise = sns.load_dataset('exercise')
pen = sns.load_dataset('penguins')

# create figures
fig, ax = plt.subplots(2, 1, figsize=(8, 8))

# plot data
g1 = 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()

enter image description here

示例2

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 labels
g1.set(title='Exercise: Pulse by Time for Exercise Type')  # add a title
g1.set(ylabel=None)  # remove the axis label

g2 = 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 label
g2.tick_params(left=False)  # remove the ticks

plt.tight_layout()
plt.show()

enter image description here

删除标签

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# sinusoidal sample data
sample_length = range(1, 1+1) # number of columns of frequencies
rads = 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)

# plot
fig, ax = plt.subplots(figsize=(8, 8))
ax.plot('radians', 'freq: 1x', data=df)

enter image description here