简体   繁体   English

难以将 str 数据转换为 int/float/decimal 以使用 matplolib 进行绘图

[英]Difficulty converting str data to int/float/decimal for plotting with matplolib

I have extracted data from a SQL database table but have been having persistent issues with trying to plot a graph between two variables.我已经从 SQL 数据库表中提取了数据,但是在尝试绘制两个变量之间的图形时一直存在问题。 This is due to conversion issue between data types.这是由于数据类型之间的转换问题。 I have first successfully converted a list to a str data type and now I want to convert it to either a float / int / decimal type so that I can use it with matplotlib.我首先成功地将list转换为str数据类型,现在我想将其转换为float / int / decimal类型,以便我可以将它与 matplotlib 一起使用。 I feel stuck at the moment since I am unable to convert the str data to any of them.我现在感到卡住了,因为我无法将str数据转换为其中任何一个。 Shown below is my script:下面显示的是我的脚本:

import mysql.connector as mariadb
import matplotlib as mpl
mpl.use('Agg')
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from matplotlib import pyplot
from decimal import Decimal
import pandas as pd

mariadb_connection = mariadb.connect(user='user', password='password', database='mydatabase')
cur = mariadb_connection.cursor()
cur.execute("SELECT time, current FROM table1")

current = []
time = []

for row in cur.fetchall():
    current.append(int(row[1]))
    time.append(str(row[0]))

print(type(time)) #at this point terminal shows it is a list data type
time = pd.to_datetime(time)
print(type(time)) #at this point terminal shows it is a <class 'pandas.core.indexes.datetimes.DatetimeIndex'>data type

print(type(current))  #at this point, terminal shows it is a list data type
current=','.join(str(v) for v in current)
print(type(current)) #at this point, terminal shows it is a str data 
current=float(current) #at this point, I get an error stating ValueError: invalid literal for float()
print(type(current))

mariadb_connection.close()

plt.figure()
plt.plot(time, current) 

plt.show()
fig.savefig('plot.jpg')

On running this script, I get an error stating ValueError: invalid literal for float(): 51,52,52,53,52,52,53,53,55,54,58,72,63,68,79,140,133,102,116,120,189,196,151,249,277,218,206,210,212,173,194,216,181,166,221,212,175,189,288,300,281,210,266在运行此脚本,我得到一个错误,说明ValueError: invalid literal for float(): 51,52,52,53,52,52,53,53,55,54,58,72,63,68,79,140,133,102,116,120,189,196,151,249,277,218,206,210,212,173,194,216,181,166,221,212,175,189,288,300,281,210,266

I then modified the line current=float(current) to current=int(current) to try converting the str data to int type and it states another error ValueError: invalid literal for int() with base 10: '51,52,52,53,.....然后我将行current=float(current)current=int(current)以尝试将str数据转换为int类型,并指出另一个错误ValueError: invalid literal for int() with base 10: '51,52,52,53,.....

I then also tried to convert my current variables to decimal type by changing that line to current=decimal(current) but I get decimal.InvalidOperation: Invalid literal for Decimal: '51,52,52....然后我还尝试通过将该行更改为current=decimal(current)来将当前变量转换为十进制类型,但我得到了decimal.InvalidOperation: Invalid literal for Decimal: '51,52,52....

Any advice on how I can convert my current values to int / float / decimal type?关于如何将current值转换为int / float / decimal类型的任何建议?

Updates : There is no need to go through the conversion steps for the current variable.更新:无需为当前变量执行转换步骤。 Apparently, just using the integer list with pandas plotting is sufficient to get a plot.显然,仅使用带有 Pandas 绘图的整数列表就足以得到一个绘图。

If you want to use pandas, this will work:如果您想使用熊猫,这将起作用:

import pandas as pd 

# this is the data you gave (only five values)  
time = ['2017-12-27 12:53:00', '2017-12-27 13:19:00', '2017-12-27 13:20:00', '2017-12-27 13:25:00', '2017-12-27 13:30:00']
current = [51, 52, 52, 53, 52]

df = pd.DataFrame(current, index=time)
df.plot()

在此处输入图片说明

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

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