pandas 如何从python中的数据帧绘制x轴和y轴的直方图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45696688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:14:51  来源:igfitidea点击:

How to plot histogram with x and y axis from dataframe in python

pythonpandashistogram

提问by ejshin1

I would like to plot histogram with pandas dataframe. I have four columns in my dataframe, but I would like to pick two of them and plot it. I plug in xaxis and yaxis values and draw three sub historgrams.

我想用Pandas数据框绘制直方图。我的数据框中有四列,但我想选择其中的两列并绘制它。我插入 xaxis 和 yaxis 值并绘制三个子直方图。

Here's how my code looks like:

我的代码如下所示:

fig = plt.figure(figsize=(9,7), dpi=100)

h = plt.hist(x=df_mean_h ['id'], y=df_mean_h ['mean'], 
color='red', label='h')

c = plt.hist(x=df_mean_c ['id'], y=df_mean_c ['mean'], 
color='blue', label='c')

o = plt.hist( x=df_mean_o['id'], y=df_mean_o ['mean'], 
color='green', label='o')

plt.show()

When I try to see the histogram, it displays nothing on the screen. How should I fix my code?

当我尝试查看直方图时,它在屏幕上不显示任何内容。我应该如何修复我的代码?

回答by name goes here

  1. You need to show the plot with plt.show()

  2. plt.hist() works differently than scatter or a series. You can't send x= and y=

  1. 您需要使用 plt.show() 显示绘图

  2. plt.hist() 的工作方式与分散或系列不同。你不能发送 x= 和 y=

https://matplotlib.org/1.2.1/examples/pylab_examples/histogram_demo.html

https://matplotlib.org/1.2.1/examples/pylab_examples/histogram_demo.html

To make your example work, just send plt.hist a single column to create the chart:

为了使您的示例工作,只需向 plt.hist 发送一列即可创建图表:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
 'two' : pd.Series([1., 2., 3.], index=['a', 'b', 'c'])}
DF = pd.DataFrame(d)
fig = plt.figure(figsize=(9,7), dpi=100)

plt.hist(DF['two'])

plt.show()