简体   繁体   English

使用 csv 中的 matplotlib 绘制数据,但 y 轴上的数字不按顺序排列

[英]Plotting data using matplotlib from csv but the numbers on the y-axis are not in order

I'm new to Python and I have been trying to plot a graph using matplotlib in PyCharm from csv file. I'm new to Python and I have been trying to plot a graph using matplotlib in PyCharm from csv file. The x-axis is months and y-axis is sales, but the numbers on the y axis are not in the right order. x 轴是月份,y 轴是销售额,但 y 轴上的数字顺序不正确。 I have read that I need to convert it to float but it says "ValueError: could not convert string to float: 'sales' ".我读过我需要将其转换为浮点数,但它显示“ValueError:无法将字符串转换为浮点数:'sales'”。 I think this is because in the csv file the header of the row with the sales data is 'sales' so it can't convert the word 'sales' to float.我认为这是因为在 csv 文件中,带有销售数据的行的 header 是“销售”,因此它无法将“销售”一词转换为浮动。 How do I make it ignore the header and convert the rest of the values to float?如何让它忽略 header 并将值的 rest 转换为浮动? Or if that's not what's wrong, can someone please help me fix it?:)或者,如果这不是问题所在,有人可以帮我解决它吗?:)

This is the code I have (without my attempt to convert to float):这是我拥有的代码(没有尝试转换为浮点数):

import matplotlib.pyplot as plt

x = []
y = []

with open('sales.csv','r') as sales_csv:
    plots = csv.reader(sales_csv, delimiter=',')
    for row in plots:
        x.append(row[1])
        y.append(row[2])

plt.plot(x, y, color='r', label='Monthly Sales 2018', marker='o')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.title('Monthly Sales 2018')
plt.legend()

plt.show()

And please find attached a screenshot of how the graph looks like.并附上图表外观的屏幕截图。 graph图形

Also, just for reference, this is the csv file (only need to plot month and sales)另外,仅供参考,这是csv文件(只需要plot月份和销量)

year, month,sales,expenditure
2018,jan,6226,3808
2018,feb,1521,3373
2018,mar,1842,3965
2018,apr,2051,1098
2018,may,1728,3046
2018,jun,2138,2258
2018,jul,7479,2084
2018,aug,4434,2799
2018,sep,3615,1649
2018,oct,5472,1116
2018,nov,7224,1431
2018,dec,1812,3532

Any help would be appreciated!任何帮助,将不胜感激!

Since the CSV has a header, you can use csv.DictReader(sales_csv) to read your CSV file.由于 CSV 有一个 header,您可以使用csv.DictReader(sales_csv)来读取您的 ZACC8D6D58C5510CDEA49 文件By default, it will read the first line in your CSV as the column names of your CSV instead of using it as a regular row.默认情况下,它将读取 CSV 中的第一行作为 CSV 的列名,而不是将其用作常规行。 Then, when you iterate over the rows, you can use row["month"] and row["sales"] to access the appropriate columns.然后,当您遍历行时,您可以使用row["month"]row["sales"]访问相应的列。

with open('sales.csv','r') as sales_csv:
    plots = csv.DictReader(sales_csv, delimiter=',')
    for row in plots:
        x.append(row["month"])
        y.append(float(row["sales"]))

Just paste your data in a file and save it as test.csv and run this.只需将您的数据粘贴到文件中并将其保存为test.csv并运行它。 Note that your second column name is ' month' and not 'month' because the data that you've pasted as of now has a space post the comma after the first column.请注意,您的第二列名称是' month'而不是'month' ,因为您现在粘贴的数据在第一列之后的逗号后面有一个空格。 Either keep that and run this code or remove that and edit this code to replace ' month' with 'month' .要么保留它并运行此代码,要么删除它并编辑此代码以将' month'替换为'month'

import pandas as pd
from matplotlib import pyplot as plt
import matplotlib.dates as mdates

# paste your data into a file and save it as test.csv
# Please note that read_csv assumes that row 0 is the header, so, 
# we don't need to pass that argument for your case
data = pd.read_csv('test.csv') 

data[' month'] = data[' month'].str.title()
data['Date'] = data[' month']
# converting type from str to pandas datetime stamps
data['Date'] = pd.to_datetime(data['Date'], format='%b')
# changing the year from 1900 (default) to 2018(desired)
data['Date'] = data['Date'].mask(data['Date'].dt.year == 1900, 
                             data['Date'] + pd.offsets.DateOffset(year=2018))

plt.plot(data['Date'], data['sales'], color='r', label='Monthly Sales 2018', marker='o')

# x-axis date representation formatting
myFmt = mdates.DateFormatter('%b')
plt.gca().xaxis.set_major_formatter(myFmt)

plt.xlabel('Month')
plt.ylabel('Sales')
plt.title('Monthly Sales 2018')
plt.legend()
plt.show()

Reference: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior参考: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

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

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