简体   繁体   English

在同一数据框中使用x和y绘制直方图

[英]Plotting histogram with x and y from the same dataframe

I'm trying to make a histogram for a dataframe: 我正在尝试为数据框制作直方图:

Date            d1    d2    h1          h2
2007-01-12      2.0   4.0   0.993508    0.984092        
2007-01-16      7.0   3.0   0.983782    0.977396    
2007-01-17      9.0   8.0   1.016174    0.999694    
...
...

My expected output is to have the d1 and d2 columns on the x axis of the histogram , and the h1 and h2 values be the frequency that gets plotted. 我的预期输出是在直方图的x轴上具有d1和d2列,并且h1和h2值是绘制的频率。

I've looked into plt.hist(), pd.DataFrame.hist() , and np.histogram() but unable to shape the data the way I'd like. 我已经研究了plt.hist(),pd.DataFrame.hist()和np.histogram(),但是无法按照我想要的方式对数据进行整形。

I get errors like len() of unsized object , range parameter must be finite., etc 我收到类似unsize对象的len()的错误,范围参数必须是有限的,等等。

It sounds like you want a barplot, not a histogram. 听起来像是您想要一个柱状图,而不是直方图。 A histogram typically acts on a collection of data and plots the frequencies for you. 直方图通常作用于数据集合并为您绘制频率。 If you want to specify the frequencies, how about something like this (assuming you have a pandas.DataFrame called df ): 如果要指定频率,那么类似这样的事情(假设您有一个名为dfpandas.DataFrame ):

import pandas
import numpy as np
from matplotlib import pyplot as plt

df = pandas.DataFrame(...)
positions = np.array(df[['d1','d2']]).flatten()
frequencies = np.array(df[['h1','h2']]).flatten()
plt.bar(positions, frequencies)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM