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
提问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
回答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()