python绘制了两个数据,但直方图上只显示一个数据

2024-03-28 08:56:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图绘制一个包含两列的柱状图,信贷和分期付款。信用只能是1或0(已批准,未批准),分期付款是他们每月支付的金额

df=pn.read_csv(loc)
credit=df['credit.policy']
ins=df['installment']
     _,b,_=plt.hist(ins,bins='auto',label='credit=1',alpha=0.5,color='blue')
plt.hist(credit,bins=b,label='credit=0',alpha=0.5,color='red')
plt.legend(loc='best')
plt.ylim([0,700])
plt.show()

the image produced

我需要制作的图像是这样的

image2

[![新代码后][3][3]


Tags: alphadf绘制plt金额lochistlabel
2条回答

我决定创建两个列表,一个是信用=0,一个是信用=1的客户

cred=[]
i0=[]
i1=[]
#df.hist(column='installment',bins='auto',label='credit=1',alpha=0.5,color='blue')
#df.hist(column='credit.policy',bins='auto',label='credit=1',alpha=0.5,color='red')
for i, row in credit.iteritems():
    cred.append(row)
for i, row in ins.iteritems():
    
    if cred[i]==1:
        i1.append(row)
    else:
        i0.append(row) 

plt.hist(i0,bins=75,color='blue',label='credit=0')
plt.hist(i1,bins=75,alpha=0.5,color='red',label='credit=1')
plt.legend()
plt.show()

image

两个样本的直方图的简单示例可能会有所帮助:

import numpy as np
import matplotlib.pyplot as plt

# Fixing random state for reproducibility
np.random.seed(19680801)

mu, sigma = 100, 15
x1 = mu + sigma * np.random.randn(10000)
x2 = (mu + sigma * np.random.randn(10000))/2
# the histogram of the data
n, bins, patches = plt.hist([x1,x2], 50, density=True, alpha=0.75)


plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.xlim(40, 160)
plt.ylim(0, 0.03)
plt.grid(True)
plt.show()

有关如何使用matplotlib的更多示例,请参见https://matplotlib.org/3.1.1/gallery/index.html

您要求的输出似乎不太常见,因为可能会与堆叠图混淆

相关问题 更多 >