[英]How to create a histogram for a dataframe with just 1 column and multiple rows (row values should be plot on x axis and column values on y axis)
[英]How to plot histogram with x and y axis from dataframe in python
我想用熊猫数据框绘制直方图。 我的数据框中有四列,但我想选择其中的两列并作图。 我插入xaxis和yaxis值并绘制三个子直方图。
这是我的代码的样子:
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()
当我尝试查看直方图时,它在屏幕上什么也不显示。 我应该如何修正我的代码?
您需要使用plt.show()显示图
plt.hist()的工作方式不同于散点图或系列。 您无法发送x =和y =
https://matplotlib.org/1.2.1/examples/pylab_examples/histogram_demo.html
为了使您的示例可行,只需将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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.