python 如何从海运左轴上去除标签?

qvtsj1bj  于 2023-06-28  发布在  Python
关注(0)|答案(1)|浏览(89)

我把三个条形图叠在一起做成了一个方形饼图。
图表正是我想要的,除了左轴标签(“狗”)。
我试过ax.set(xlabel=None)-这不起作用。
我尝试添加一个没有名称且值为零的行,但这没有任何影响。
我试着切换x和y值的位置,按照here,但这没有影响。
下面是我的代码:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'index': ['Dogs', 'Birds', 'Cats'],
    'percent': [20.444444, 53.629630, 100.000000],
})

#split dataframe into individual barplots
df_1 = df.iloc[[0]]
df_2 = df.iloc[[1]]
df_3 = df.iloc[[2]]

sns.set_theme(style="whitegrid")

#set size
f, ax = plt.subplots(figsize=(30, 10))

#make barplots

sns.barplot(x="percent", y="index", data=df_3,
            color='#d7301f',ax=None)

sns.barplot(x="percent", y="index", data=df_2,
            color='#fc8d59',ax=None)

sns.barplot(x="percent", y="index", data=df_1,
            color='#fdcc8a',ax=None)

#add text

ax.text(0.1, 0.5, "Dogs (20%)", ha="center", va="center", transform=ax.transAxes,color='#313639',fontsize=30)

ax.text(0.35, 0.5, "Birds (33%)", ha="center", va="center", transform=ax.transAxes,color='#313639',fontsize=30)

ax.text(0.76, 0.5, "Cats (46%)", ha="center", va="center", transform=ax.transAxes,color='#313639',fontsize=30)

#remove ticks and labels
ax.set(xticklabels=[])
ax.set(xlabel=None)
ax.set(ylabel=None)

sns.despine(bottom = True, left = True)
ax.grid(False)
o2gm4chl

o2gm4chl1#

这实际上是一个tick标签,所以ax.set(yticklabels=[])将完成这项工作!
完整代码:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'index': ['Dogs', 'Birds', 'Cats'],
    'percent': [20.444444, 53.629630, 100.000000],
})

#split dataframe into individual barplots
df_1 = df.iloc[[0]]
df_2 = df.iloc[[1]]
df_3 = df.iloc[[2]]

sns.set_theme(style="whitegrid")

#set size
f, ax = plt.subplots(figsize=(30, 10))

#make barplots

sns.barplot(x="percent", y="index", data=df_3,
            color='#d7301f',ax=ax)

sns.barplot(x="percent", y="index", data=df_2,
            color='#fc8d59',ax=ax)

sns.barplot(x="percent", y="index", data=df_1,
            color='#fdcc8a',ax=ax)
ax.set(yticklabels=[])
plt.show()

相关问题