资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

PythonMatplotlib绘图数据点位置错乱问题解决方法-创新互联

小编这次要给大家分享的是Python Matplotlib绘图数据点位置错乱问题解决方法,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

成都创新互联公司主要从事网站设计制作、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务海州,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792

在绘制正负样本在各个特征维度上的CDF(累积分布)图时出现了以下问题:

Python Matplotlib绘图数据点位置错乱问题解决方法

问题具体表现为:

1.几个负样本的数据点位置倒错

2.X轴刻度变成了乱七八糟一团鬼东西

最终解决办法

造成上述情况的原因其实是由于输入matplotlib.plot()函数的数据x_data和y_data从CSV文件中直接导入后格式为string,因此才会导致所有数据点的x坐标都被直接刻在了x轴上,且由于坐标数据格式错误,部分点也就表现为“乱点”。解决办法就是导入x,y数据后先将其转化为float型数据,然后输入plot()函数,问题即解决。

Python Matplotlib绘图数据点位置错乱问题解决方法

补充知识:matplotlib如何在绘制时间序列时跳过无数据的区间

其实官方文档里就提供了方法,这里简单的翻译并记录一下.

11.1.9 Skip dates where there is no data
When plotting time series, e.g., financial time series, one often wants to leave out days on which there is no data, e.g., weekends.
By passing in dates on the x-xaxis, you get large horizontal gaps on periods when there is not data.

The solution is to pass in some proxy x-data, e.g., evenly sampled indices, and then use a custom formatter to format these as dates.
The example below shows how to use an ‘index formatter' to achieve the desired plot:

解决方案是通过传递x轴数据的代理,比如下标,

然后通过自定义的'formatter'去取到相对应的时间信息

manual内示例代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.ticker as ticker

#读数据
r = mlab.csv2rec('../data/aapl.csv')
r.sort()
r = r[-30:] # get the last 30 days
N = len(r)
ind = np.arange(N) # the evenly spaced plot indices
def format_date(x, pos=None):
 #保证下标不越界,很重要,越界会导致最终plot坐标轴label无显示
 thisind = np.clip(int(x+0.5), 0, N-1)
 return r.date[thisind].strftime('%Y-%m-%d')

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(ind, r.adj_close, 'o-')
ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
fig.autofmt_xdate()
plt.show()

本文标题:PythonMatplotlib绘图数据点位置错乱问题解决方法-创新互联
网页链接:http://cdkjz.cn/article/pdsps.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220