我正在用这个代码构建一个海图:
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
%matplotlib inline
df=pd.DataFrame(data=['a','b','c','d','a','a','b','c','a','a'],columns['datos'])
tabla=df['datos'].value_counts().reset_index()
fig, ax1 = plt.subplots(figsize=(6,4))
sns.set()
sns.barplot(x='index', y='datos', data=tabla, ax=ax1)
它的工作properly....but我怎么才能得到代码的颜色使用的每一条?我已经尝试过了:
f.get_color()
ax.get_color()
但是一点都不成功。
提前谢谢瓦利
发布于 2018-07-23 04:20:05
我承认这是一种丑陋的方式,但你需要访问你的情节的孩子。请注意,如果您绘制的不仅仅是countplot,这可能不会起作用,因为您将在绘图中获得所有使用的矩形颜色。
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import pandas as pd
import seaborn as sns
%matplotlib inline
sns.set()
df=pd.DataFrame(data=['a','b','c','d','a','a','b','c','a','a'],columns=['datos'])
tabla=df['datos'].value_counts().reset_index()
fig, ax1 = plt.subplots(figsize=(6,4))
sns.barplot(x='index', y='datos', data=tabla, ax=ax1)
bars = [r for r in ax1.get_children() if type(r)==Rectangle]
colors = [c.get_facecolor() for c in bars[:-1]] # I think the last Rectangle is the background.
https://stackoverflow.com/questions/51468670
复制相似问题