【发布时间】:2018-06-13 16:29:56
【问题描述】:
我编写了一个代码,它给出了一个累积标准化直方图。
请问如何固定X轴?
此直方图具有应用于第二个维度的阈值的附加特征,因此可以使用有关“B”列的信息以及“A”列。
它还可以调整计数标准化的数字“C”。
import pandas as pd
import numpy as np
# Data
df_1 = pd.DataFrame({'A': [1,2,1,2,3,4,2,1,4],
'B': [2,1,2,1,2,3,4,2,1]})
# Cumulative Normed Histogram
bins = np.arange(0, 5, .2)
df_1['A_Bin'] = pd.cut(df_1['A'], bins=bins)
# Apply a threshold to B
df_2 = df_1[df_1['B'] > 2]
# Get the number of rows
C = len(df_1.index)
def fun(g):
try:
return float(g.shape[0]) / C
except ZeroDivisionError:
return np.nan
hist = df_1.groupby('A_Bin').apply(fun)
hist_2 = df_2.groupby('A_Bin').apply(fun)
hist_cum = hist.cumsum()
hist_2_cum = hist_2.cumsum()
hist_cum.plot()
hist_2_cum.plot()
我试过这个:
import matplotlib.pyplot as plt
plt.xticks((0,2,4,6),('0','2','4','6'))
但是得到了这个:
【问题讨论】:
标签: python histogram cumulative-sum