我怎样才能在 python 上提前绘制?

问题描述 投票:0回答:1

I have RoomAvr data of two buildings. Here are some example rows:

I want to plot line chart like this

legnd

I try ax.plot(x, y, label='quadratic') BUT work like this

我该如何开发这段代码?

python
1个回答
0
投票

好吧,因为没有发布代码可言,我决定快速举个例子:

import numpy as np

# create a sample data array
data = np.array([
    ['data', 7122, '24/02/2023 15:51', 22.06071, 22],
    ['data', 7122, '24/02/2023 15:22', 22.022, 22],
    ['data', 7122, '24/02/2023 15:00', 22.011, 22],
    # add more rows here
])

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

# now plot the data with row 2 as x-axis and row 3 as y-axis

# convert the np array to a pandas dataframe
df = pd.DataFrame(data)

# convert the 3rd column to datetime
df[2] = pd.to_datetime(df[2])

# plot the data
fig, ax = plt.subplots()
ax.plot(df[2], df[3])

# set the x-axis to be date
ax.xaxis.set_major_locator(mdates.DayLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
# set the x-axis label
ax.set_xlabel('Time')

# rotate the x-axis labels
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
# set the y-axis label
ax.set_ylabel('Room Avr')
# set the title
ax.set_title('Temperature over time')
# show the plot
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.