- You can refer to this article: < font size = "3" > Python visualization plotly draw scatterplot < / font > < / a > < / li >
Too much data 1 second. Good day 9w data. The data is graphed out as a line graph, which is a bar graph. How can we reduce the interval such as database query every 10 to get one. Or plotly when drawing a graph, do it every 10 times
import os
import numpy as np
import pandas as pd
import sqlite3
import plotly as py
import plotly.graph_objs as go
#import plotly.expression as px
from plotly import tools
import datetime
import warnings
warnings.filterwarnings("ignore")
# 使用8266 dht11做到温湿度采集。上传到腾讯我的vos服务器,自己做的http接受字符串程序
# 使用8266 dht11做到温湿度采集。上传到腾讯我的vos服务器,自己做的http接受字符串程序
#建立数据库连接
#conn = pymysql.connect("192.168.6.128","root","root","test_db",charset="utf8")
con=sqlite3.connect("220604_temp_dht11.db")
print("连接成功")
#读取数据库表数据
cursor = con.cursor()
#dataCopy = cursor.execute("select * from t1 where sdate>'2022-05-04' order by id desc ")
#dataCopy = cursor.execute("select * from t1 order by id desc Limit 15000 ")
dataCopy = cursor.execute("select * from t1 order by id desc ")
#data = pd.read_sql("select * from t1 Limit 100 order by id desc ",con=con)
values = dataCopy.fetchall()
#for row in values:
# 使用Pandas的DataFrame来处理每一行要比使用一个包含元组的元组方便
# 下面的Python代码片段将所有行转化为DataFrame实例
df = pd.DataFrame([[ij for ij in i] for i in values])
# print(df)
# 步骤一
df.rename(columns={0: 'id', 1: 'tt', 2: 'hh', 4: 'sdate'}, inplace=True)
#xx=datetime.datetime.strftime(df["sdate"],'%Y-%m-%d')
trace0 = go.Scatter(x=df["sdate"],y=df["tt"],name="温度")
trace1 = go.Scatter(x=df["sdate"],y=df["hh"],name="湿度")
# 步骤二
data = [trace0,trace1]
# 步骤三
fig = go.Figure(data)
# 步骤四
fig.update_layout(
xaxis_title="年份",
yaxis_title="温湿度"
)
# 步骤五
output_path = "C:/bar.html"
py.offline.plot(fig, filename=output_path)
#fig.show()
0 Answer
How to smooth out this data graph. Like average every 10 numbers, or something to smooth the output a little bit. Let me draw it again. The current graph is too choppy
It's really OK. The big God should answer quote ChatGPT. What does that mean. Is it gtp? How do you know the name of this function?
is Chatgpt give solution
This answer refers to ChatGPT
Refer to the code below
import os
import numpy as np
import pandas as pd
import sqlite3
import plotly.graph_objs as go
from plotly import tools
import datetime
import warnings
warnings.filterwarnings("ignore")
# 建立数据库连接
con = sqlite3.connect("220604_temp_dht11.db")
print("连接成功")
# 读取数据库表数据
cursor = con.cursor()
dataCopy = cursor.execute("select * from t1 order by id desc")
# 将数据存入Pandas DataFrame中
df = pd.DataFrame([[ij for ij in i] for i in dataCopy.fetchall()])
df.rename(columns={0: 'id', 1: 'tt', 2: 'hh', 4: 'sdate'}, inplace=True)
# 取每隔10个数据只取1个
df = df.iloc[::10, :]
# 创建Trace
trace0 = go.Scatter(x=df["sdate"], y=df["tt"], name="温度", mode='lines+markers')
trace1 = go.Scatter(x=df["sdate"], y=df["hh"], name="湿度", mode='lines+markers')
# 创建Data和Layout
data = [trace0, trace1]
layout = go.Layout(xaxis_title="年份", yaxis_title="温湿度")
# 创建Figure
fig = go.Figure(data, layout)
# 输出图形
output_path = "C:/bar.html"
fig.write_html(output_path)
这家伙很懒,什么都没留下...