matplotlib中的行之一不可见

时间:2018-08-04 15:59:52

标签: python python-3.x numpy matplotlib data-visualization

from datetime import datetime
import datetime as dt
import csv    
import matplotlib.dates as mdates
import numpy as np
import matplotlib.pyplot as plt

ldays = list()
aesvalues = list()
tezvalues = list()
vdevalues = list()

with open('data/dg_year_50.csv', newline='') as csvfile:
    has_header = csv.Sniffer().has_header(csvfile.read(1024))
    csvfile.seek(0)  # Rewind.
    datareader = csv.reader(csvfile, delimiter=',', quotechar='|')
    if has_header:
        next(datareader)  # Skip header row.

    for row in datareader:
        date_cell = row[0]
        aes_cell = row[1]
        tez_cell = row[2]
        vde_cell = row[3]
        datetime_object = datetime.strptime(date_cell, '%d.%m.%Y %H:%M:%S')
        aesvalues.append(aes_cell)
        tezvalues.append(tez_cell)
        vdevalues.append(tez_cell)
        ldays.append(datetime_object)

days = mdates.date2num(np.array(ldays))

s_aesvalues = sorted(list(map(int, aesvalues)))
s_tezvalues = sorted(list(map(int, tezvalues)))
s_vdevalues = sorted(list(map(int, vdevalues)))


# These are the colors that will be used in the plot
color_sequence = ['#ffbb78', '#e377c2', '#1f77b4', '#ff7f0e', '#2ca02c',
              '#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5',
              '#8c564b', '#c49c94', '#aec7e8', '#e377c2', '#f7b6d2',
              '#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5']

# make a plot
fig, ax = plt.subplots(figsize=(14, 12))

# plot the data
majors = [s_tezvalues, s_vdevalues, s_aesvalues]

for rank, column in enumerate(majors):
    line = plt.plot_date(x=days, y=majors[rank], lw=2.5,    color=color_sequence[rank])

fig.suptitle('Дані по енергетиці', fontsize=18, ha='center')
plt.xlabel('Дата', fontsize=14, ha='center')
plt.ylabel('Кількість енергії (Вт)', fontsize=14, ha='center')

plt.show()

我有4列(Дата,АЕС,ТЕЦ,ВДЕ)“ data / dg_year_50.csv”。 第一个代表“日期”,其余三个代表需要以图表中的线形式显示的适当值。 我不明白为什么我要显示2行而不是3行。 结果可视化看起来像这样:

1 个答案:

答案 0 :(得分:0)

我发现了错误,它在以下字符串中:

vdevalues.append(tez_cell)

应该这样更改:

vdevalues.append(vde_cell)

是的,当然,使用熊猫解决方案要容易得多:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv('data/dg_year_50.csv', index_col=0, parse_dates=True).apply(np.int64)
data.plot()
plt.show()

附加结果图: