【问题标题】:Python Matplotlib/Seaborn/Jupyter - Putting bar plot in wrong place?Python Matplotlib/Seaborn/Jupyter - 将条形图放在错误的位置?
【发布时间】:2019-09-09 10:13:53
【问题描述】:

我在 Jupyter 笔记本中使用以下内容,使用最新的 Anaconda 更新(包括 Matplotlib 3.1.1)

感谢 SpghttCd,我有 code to do a stacked horizontal bar,但 Seaborn 将它放在默认图下方的新图上。

我怎样才能最好地解决这个问题?

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

data=pd.DataFrame(data={"R1":["Yes","Yes","Yes","No","No"]})
freq = data["R1"].value_counts(normalize=True)*100
fig,ax = plt.subplots()
freq.to_frame().T.plot.barh(stacked=True)

【问题讨论】:

  • 删除fig,ax = plt.subplots()

标签: python matplotlib jupyter-notebook seaborn


【解决方案1】:

您会在 Jupyter 中看到两个轴,因为您使用 plt.subplots() 创建了一个新轴,而 pandas 也创建了另一个轴。

如果您需要重复使用现有的斧头,请将其传递给使用ax 开关的绘图方法:

fig, axe = plt.subplots()
freq.to_frame().T.plot.barh(stacked=True, ax=axe)

详见 pandas 文档,绘图方法总是显示ax 开关:

ax : Matplotlib 轴对象,可选

如果您接受 pandas 为您创建它,正如 @Bharath M 建议的那样,只需发出:

axe = freq.to_frame().T.plot.barh(stacked=True)

然后您将看到一个独特的轴,您可以通过变量axe 访问它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多