【发布时间】:2018-03-11 21:39:28
【问题描述】:
我想从pandas Dataframe 用以下列绘制直方图(染色体之间重叠和非重叠)。
my_cols = ['chrom', 'len_PIs']
chrom = pd.Series(['chr1', 'chr2', 'chr3'])
len_of_PIs = pd.Series([[np.random.randint(15, 59, 86)],
[np.random.randint(18, 55, 92)],
[np.random.randint(25, 61, 98)]])
my_df = pd.DataFrame({'chrom': chrom,
'len_PIs': len_of_PIs},
columns=my_cols)
print('\nhere is df5')
print(df5)
print(type(df5))
print(type(df5['len_PIs']))
here is df5
chrom len_PIs
0 chr1 [[18, 45, 33, 58, 48, 47, 45, 39, 42, 46, 48, ...
1 chr2 [[45, 32, 49, 46, 53, 40, 46, 35, 44, 24, 51, ...
2 chr3 [[53, 32, 35, 35, 49, 31, 57, 42, 46, 49, 49, ...
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.series.Series'>
所以,现在我想为每个chrom 使用len_PIs 值制作直方图。
import matplotlib.pyplot as plt
with open('histogram_byChr.png', 'w'):
fig = plt.figure()
plt.subplot()
plt.xlabel('chrom')
plt.ylabel('len_PIs')
fig.suptitle('length of PIs distribution for each chromosome')
# these two method (below) are close but don't work
plt.plot(my_df.groupby('chrom')['len_PIs'])
# error message which doesn't make sense to me
ValueError: could not convert string to float: 'chr3'
my_df.groupby('chrom').plot.hist(alpha=0.5)
# Error message
TypeError: Empty 'DataFrame': no numeric data to plot
【问题讨论】:
-
您的示例代码仅生成 NaN。可以再看一遍吗?
-
我刚刚更新了代码。我打错了
len_PIs,但这不是我原始代码中的问题。我尝试查看其他拼写错误,但没有发现任何问题。如果看到其他问题,请告诉我。谢谢。
标签: python python-3.x pandas matplotlib histogram