【发布时间】:2016-03-21 21:11:54
【问题描述】:
我已经阅读了以下页面,
- http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html
- http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.barh
- http://matplotlib.org/users/customizing.html
- http://matplotlib.org/users/configuration.html
但我仍然无法自定义图表的详细设置。
对于simple code,
#%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')
df = pd.DataFrame({
'person':[x*16 for x in list('ABCDEF')],
'score1':np.random.randn(6),
'score2':np.random.randn(6),
'score3':np.random.randn(6),
'score4':np.random.randn(6),
'score5':np.random.randn(6)
})
print(df)
plt.close('all') # close all open figures
fig, ax = plt.subplots()
# X: pd.options.display.mpl_style = 'default' # cause system freeze
df.set_index(['person']).plot(kind='barh', ax = ax, width=0.85, fontsize=8)
ax.invert_yaxis()
plt.show()
结果如下:
也就是说,我所有的 y 标签都被切断了,而且边距太大。我在这里找到了如何调整它们:
但我想知道如何以编程方式进行操作。
谢谢
【问题讨论】:
-
在
plt.show之前调用plt.tight_layout()对您有用吗? -
是的!极好的!!奇怪的是,我之前从 GUI 中单击它,结果之前得到了非常奇怪的结果。奇怪的。无论如何,请回答,以便我标记。谢谢。
-
哦,@johnchase,您是否也知道如何禁用 y 网格线?我找到了
ax.yaxis.set_visible(False),但是当我尝试它时,我的 y 标签也不见了。谢谢。 -
知道了,是
ax.yaxis.grid(False)
标签: pandas matplotlib plot bar-chart