简体   繁体   English

简单图不代表数据

[英]Simple Graph Does Not Represent Data

This is a very straightforward question. 这是一个非常简单的问题。 I have and x axis of years and ay axis of numbers increasing linearly by 100. When plotting this with pandas and matplotlib I am given a graph that does not represent the data whatsoever. 我的x轴和年份的y轴以及数字的y轴线性增加100。使用pandas和matplotlib进行绘制时,会得到一个不表示任何数据的图形。 I need some help to figure this out because it is such a small amount of code: 我需要一些帮助来解决这个问题,因为它的代码很少:

The CSV is as follows: CSV如下:

A,B
2012,100
2013,200
2014,300
2015,400
2016,500
2017,600
2018,700
2012,800
2013,900
2014,1000
2015,1100
2016,1200
2017,1300
2018,1400

The Code: 编码:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

data = pd.read_csv("CSV/DSNY.csv")

data.set_index("A", inplace=True)


data.plot()
plt.show()

The graph this yields is: 产生的图形是:

CSV数据中的图形

It is clearly very inconsistent with the data - any suggestions? 显然与数据不一致-有什么建议吗?

The default behaviour of matplotlib/pandas is to draw a line between successive data points, and not to mark each data point with a symbol. matplotlib / pandas的默认行为是在连续的数据点之间画一条线,而不是用符号标记每个数据点。

Fix: change data.plot() to data.plot(style='o') , or df.plot(marker='o', linewidth=0) . 修复:data.plot()更改为data.plot(style='o')df.plot(marker='o', linewidth=0)

Result: 结果: style ='o'的结果

All you need is sort A before plotting. 您需要做的是在绘图前进行A类排序。

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

data = pd.read_csv("CSV/DSNY.csv").reset_index()
data = data.sort_values('A')
data.set_index("A", inplace=True)


data.plot()
plt.show()

在此处输入图片说明

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

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