[英]Python Matplotlib/Seaborn/Jupyter - Putting bar plot in wrong place?
I'm using the following in a Jupyter notebook, using the latest Anaconda update (including Matplotlib 3.1.1,) 我正在Jupyter笔记本中使用以下内容,并使用了最新的Anaconda更新(包括Matplotlib 3.1.1)。
Thanks to SpghttCd, I have the code to do a stacked horizontal bar , but Seaborn puts it on a new plot below the default one. 多亏了SpghttCd,我有了编写堆叠的水平条的代码 ,但是Seaborn将其放置在默认图下面的新图上。
How might I best fix this problem? 我如何才能最好地解决此问题?
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)
You see two axes in Jupyter because you create a fresh one with plt.subplots()
and pandas also creates another one. 您会在Jupyter中看到两个轴,因为您使用
plt.subplots()
创建了一个新轴,而pandas也创建了另一个。
If you need to reuse an existing axe, pass it to plotting method using ax
switch: 如果需要重用现有的轴,请使用
ax
开关将其传递给绘图方法:
fig, axe = plt.subplots()
freq.to_frame().T.plot.barh(stacked=True, ax=axe)
See pandas documentation for details, plotting method always exhibits an ax
switch: 有关详细信息,请参见pandas文档。绘图方法始终会显示一个
ax
开关:
ax
: Matplotlib axis object, optionalax
:Matplotlib轴对象,可选
If you accept pandas creates it for you, as @Bharath M suggested, just issue: 如果您接受大熊猫为您创建的代码(如@Bharath M所建议的那样),请发出:
axe = freq.to_frame().T.plot.barh(stacked=True)
Then you will see an unique axes and you can access it trough the variable axe
. 然后,您将看到一个唯一的轴,并且可以通过变量
axe
访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.