从csv flie绘制图表

时间:2018-02-20 12:13:36

标签: python csv

我是python的新手。我已经从我创建的csv文件中绘制了图表。 a)每月销售额与产品价格 b)地理区域与客户编号

我实施的代码是

import pandas as pd
import matplotlib.pyplot as plot
import csv

data = pd.read_csv('dataset_books.csv')
data.hist(bins=90)
plot.xlim([0,115054])
plot.title("Data")
x = plot.xlabel("Monthly Sales")
y = plot.ylabel("Product Price")
plot.show()

我得到的输出不是我预期的,也没有被批准。 enter image description here

我需要一个带线图的水平直方图。

Book ID Product Name    Product Price   Monthly Sales   Shipping Type   Geographic Region   No Of Customer Who Bought the Product   Customer Type
1   The Path to Power   486 2566.08 Free    Gatton  4   Old
2   Touching Darkness (Midnighters, #2) 479 1264.56 Paid    Hooker Creek    2   New
3   Star Wars: Lost Stars   456 1203.84 Paid    Gladstone   2   New
4   Winter in Madrid    454 599.28  Paid    Warruwi 1   New
5   Hairy Maclary from Donaldson's Dairy    442 2333.76 Free    Mount Gambier   4   Old
6   Stand on Zanzibar   413 3816.12 Free    Cessnock    7   Old
7   Marlfox 411 3797.64 Free    Edinburgh   7   Old
8   The Matlock Paper   373 3446.52 Free    Gladstone   7   Old
9   Tears of a Tiger    361 1906.08 Free    Melbourne   4   Old
10  Star Wars: Vision of the Future 355 937.2   Paid    Wagga Wagga 2   New
11  Nefes Nefese    344 454.08  Paid    Gatton  1   New

这是我的CSV文件。 任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

请尝试检查列名:

df.columns
>> Index(['Book ID', 'Product Name', 'Product Price', 'Monthly Sales',
       'Shipping Type', 'Geographic Region',
       'No Of Customer Who Bought the Product', 'Customer Type'],
     dtype='object')

在绘制水平情节旁边,您需要'barh'。

df['Product Price'].plot(kind='barh')

enter image description here

选择列的另一个选择是'iloc'

df.iloc[:, 2].plot(kind='barh')

它将生成相同的输出