我正在成功运行代码,显然每次运行此代码时python内核都会死掉: 是代码有问题还是问题更严重?我可以运行没有其他问题的笔记本。在
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
plt.rcParams['text.usetex'] = False
df = pd.DataFrame(np.random.uniform(size=37)*100, columns=['A'])
ax = plt.figure(figsize=(10,5))
plt.barh(df.index, df['A'], color='ForestGreen')
plt.yticks(df.index)
def annotateBars(row, ax=ax):
if row['A'] < 20:
color = 'black'
horalign = 'right'
horpad = 2
else:
color = 'white'
horalign = 'right'
horpad = -2
ax.text(row.name, row['A'] + horpad, "{:.1f}%".format(row['A']),
color=color,
horizontalalignment=horalign,
verticalalignment='center',
fontsize=10)
junk = df.apply(annotateBars, ax=ax, axis=1)
在对我的代码进行了多次尝试之后:
结果是:
也许你应该改变问题的标题,因为对你来说,内核为什么会死并不重要。据我所知,问题是:
创建具有不同条形图颜色和每个条形图值的水平条形图作为批注。在
下面是一个使用Seaborn的解决方案:
创建:
更新: 也可为文本着色:
^{pr2}$使绘图变大以便背景也覆盖注释:
相关问题 更多 >
编程相关推荐