![](/img/trans.png)
[英]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.