亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 matplotlib/seaborn 圖中刪除或隱藏 y 軸刻度標簽

如何從 matplotlib/seaborn 圖中刪除或隱藏 y 軸刻度標簽

寶慕林4294392 2023-07-18 10:12:21
我做了一個看起來像這樣的情節我想關閉 y 軸上的刻度標簽。為此,我正在使用plt.tick_params(labelleft=False, left=False)現在劇情是這樣的。即使標簽被關閉,秤1e67仍然存在。關閉比例1e67會讓情節看起來更好。我怎么做?
查看完整描述

1 回答

?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

  • 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

實施例1

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()

http://img1.sycdn.imooc.com/64b5f554000189dc05090476.jpg

刪除標簽

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()

http://img4.sycdn.imooc.com/64b5f5630001c80705750565.jpg

實施例2

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)


# or skip the previous two lines and plot df directly

# ax = df.plot(x='radians', y='freq: 1x', figsize=(8, 8), legend=False)

http://img1.sycdn.imooc.com/64b5f57200015a9903790254.jpg

刪除標簽

# plot

fig, 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 labels

ax.tick_params(left=False)? # remove the ticks

http://img1.sycdn.imooc.com/64b5f57f000199c603570249.jpg

查看完整回答
反對 回復 2023-07-18
  • 1 回答
  • 0 關注
  • 681 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號