简体   繁体   中英

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.

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()

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

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:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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