Python:使用给定的列为带有 x 轴的 Pandas 数据框绘制条形图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39278279/
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 01:56:29  来源:igfitidea点击:

Python: Plot a bar graph for a pandas data frame with x axis using a given column

pythonpandasjupyter-notebookpython-ggplot

提问by Edamame

I want to plot a bar chart for the following pandas data frame on Jupyter Notebook.

我想在 Jupyter Notebook 上为以下 Pandas 数据框绘制条形图。

      | Month | number
-------------------------
    0 | Apr   |  6.5
    1 | May   |  7.3
    2 | Jun   |  3.9
    3 | Jul   |  5.1
    4 | Aug   |  4.1

I did:

我做了:

%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
trend_df.plot(kind='bar')

How do I make sure x-axis is actually showing month here?

我如何确保 x 轴在此处实际显示月份?

回答by lanery

You can simply specify xand yin your call to plotto get the bar plot you want.

您可以简单地指定xy在您的调用plot中获得您想要的条形图。

trend_df.plot(x='Month', y='number', kind='bar')

enter image description here

在此处输入图片说明

Given trend_dfas

给定trend_df

In [20]: trend_df
Out[20]: 
  Month  number
0   Apr     6.5
1   May     7.3
2   Jun     3.9
3   Jul     5.1
4   Aug     4.1

回答by SUNITHA K

store the data in a csv file.

example i named my file plot.csv

    save data in following format in plot.csv

Month,number

Apr,6.5

May,7.3

Jun,3.9

Jul,5.1

Aug,4.1


import pandas as pd

import matplotlib.pyplot as plt

import numpy as np

import csv

#first read the data
data = pd.read_csv('plot.csv',sep=',')

print(data)
#create  a data frame
df = data.ix[-5:,['Month','number']]
#plot
df.plot(kind = 'bar')

plt.show()
#for ggplot
plt.style.use('ggplot')

df.plot()

plt.show()