我试图从数据帧绘制几个系列的直方图.系列具有不同的最大值:
df[[
'age_sent', 'last_seen', 'forum_reply', 'forum_cnt', 'forum_exp', 'forum_quest'
]].max()
收益:
age_sent 1516.564016
last_seen 986.790035
forum_reply 137.000000
forum_cnt 155.000000
forum_exp 13.000000
forum_quest 10.000000
当我尝试plot histograms时,我使用sharex = False,subplots = True但看起来像sharex属性被忽略:
df[[
'age_sent', 'last_seen', 'forum_reply', 'forum_cnt', 'forum_exp', 'forum_quest'
]].plot.hist(figsize=(20, 10), logy=True, sharex=False, subplots=True)
我可以清楚地分别绘制每一个,但这不太可取.我也想知道我做错了什么.
我所包含的数据太大了,但很容易创建类似的东西:
ttt = pd.DataFrame({'a': pd.Series(np.random.uniform(1, 1000, 100)), 'b': pd.Series(np.random.uniform(1, 10, 100))})
我现在有:
ttt.plot.hist(logy=True, sharex=False, subplots=True)
检查x轴.我希望它是这样的(但是使用带有子图的一个命令).
ttt['a'].plot.hist(logy=True)
ttt['b'].plot.hist(logy=True)
解决方法:
如果平移/缩放一个轴改变另一个轴,则sharex(最有可能)恰好落到mpl并设置.
您遇到的问题是所有直方图都使用相同的容器(如果我正确理解代码,则由https://github.com/pydata/pandas/blob/master/pandas/tools/plotting.py#L2053强制执行)因为pandas假设如果你有多个直方图,那么你可能正在绘制类似数据的列,所以使用相同的分箱使它们具有可比性.
假设你有mpl> = 1.5和numpy> = 1.11,你应该给自己写一个小帮手函数,比如
import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
import numpy as np
plt.ion()
def make_hists(df, fig_kwargs=None, hist_kwargs=None,
style_cycle=None):
'''
Parameters
----------
df : pd.DataFrame
Datasource
fig_kwargs : dict, optional
kwargs to pass to `plt.subplots`
defaults to {'fig_size': (4, 1.5*len(df.columns),
'tight_layout': True}
hist_kwargs : dict, optional
Extra kwargs to pass to `ax.hist`, defaults
to `{'bins': 'auto'}
style_cycle : cycler
Style cycle to use, defaults to
mpl.rcParams['axes.prop_cycle']
Returns
-------
fig : mpl.figure.figure
The figure created
ax_list : list
The mpl.axes.Axes objects created
arts : dict
maps column names to the histogram artist
'''
if style_cycle is None:
style_cycle = mpl.rcParams['axes.prop_cycle']
if fig_kwargs is None:
fig_kwargs = {}
if hist_kwargs is None:
hist_kwargs = {}
hist_kwargs.setdefault('log', True)
# this requires nmupy >= 1.11
hist_kwargs.setdefault('bins', 'auto')
cols = df.columns
fig_kwargs.setdefault('figsize', (4, 1.5*len(cols)))
fig_kwargs.setdefault('tight_layout', True)
fig, ax_lst = plt.subplots(len(cols), 1, **fig_kwargs)
arts = {}
for ax, col, sty in zip(ax_lst, cols, style_cycle()):
h = ax.hist(col, data=df, **hist_kwargs, **sty)
ax.legend()
arts[col] = h
return fig, list(ax_lst), arts
dist = [1, 2, 5, 7, 50]
col_names = ['weibull $a={}$'.format(alpha) for alpha in dist]
test_df = pd.DataFrame(np.random.weibull(dist,
(10000, len(dist))),
columns=col_names)
make_hists(test_df)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。