简体   繁体   English

如何在Python中从数据框绘制带有x和y轴的直方图

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

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. 我插入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? 我应该如何修正我的代码?

  1. You need to show the plot with plt.show() 您需要使用plt.show()显示图

  2. plt.hist() works differently than scatter or a series. plt.hist()的工作方式不同于散点图或系列。 You can't send x= and y= 您无法发送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()

暂无
暂无

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

相关问题 如何为只有 1 列和多行的数据框创建直方图(行值应绘制在 x 轴上,列值应绘制在 y 轴上) - 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) 如何绘制直方图,yaxis 为“对应于每个 x bin 的 y 值的总和”,x 轴为 python 中 x 的 n 个 bin? - How to plot a histogram with the yaxis as the "total of y values corresponding to each x bin" and x axis as n bins of x in python? 如何在Python中打印直方图的y轴值? - How to print y-axis values of histogram plot in Python? 如何在 Python 中更改直方图 Y 轴上的值 - How can I change the values on Y axis of Histogram plot in Python 来自 .txt 文件的数据的 Plot 直方图,其中 x 轴上的名称和 y 轴上的数字 - Plot histogram for data from .txt file with names on x-axis and number on y axis 如何为 python 中 xy 平面上的网格单元格 plot 直方图? - how to plot histogram for cells of grid on x-y plane in python? 在同一数据框中使用x和y绘制直方图 - Plotting histogram with x and y from the same dataframe 如何在X轴上相对于Y值绘制日期和时间(Python) - How to plot date and time in X axis against Y value (Python) 如何从数据框中绘制直方图 - how to plot a histogram from a dataframe 绘制数据框的轮廓图,x 轴作为日期时间,y 轴作为深度 - Plotting Contour plot for a dataframe with x axis as datetime and y axis as depth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM