【问题标题】:how can i draw this graph in mathplotlib?如何在 matplotlib 中绘制此图?
【发布时间】:2021-10-02 19:30:26
【问题描述】:

我想使用 Jupyter Notebook 使用 Matplotlib 在屏幕上绘制图形。我想在横轴上显示日期,在纵轴上显示数据。我可以在垂直轴上显示数据。但是,在水平轴上,它显示日期的序列号,而不是日期。我该如何解决这个问题?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from seaborn import heatmap
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.preprocessing import MinMaxScaler
from sklearn.neural_network import MLPRegressor
import matplotlib.style as mplstyle

这里我已经安装了库。然后我提取数据并进行处理。

plt.style.use('seaborn-white')
csv_file_url = 'http://www.facadium.com.tr/cvd19/nisansonunakadarolanveriler.csv'
dataset = pd.read_csv(csv_file_url, sep = ',', index_col=0)
pd.set_option("display.max.rows", None)
dataset.head(2000)

然后我绘制了整个数据集中的数据。

for i, col in enumerate(dataset.columns.tolist()):
    plt.figure(figsize=(10, 6))
    x_axis = dataset.index.values
    y_axis = dataset[col].values
    plt.plot(x_axis, y_axis, label=col,marker='o')
    plt.title(col)
    plt.xlabel("Günler")
    plt.ylabel("Veriler")
    counter = 0
    for a, b in zip(x_axis, y_axis):
        counter +=1
        if counter % 60 == 0:
            plt.annotate(str(b), xy=(a,b))

我列出了数据。

dataset_shifted = dataset.iloc[0:,:]
dataset_shifted.head(2000)
dataset_shifted.describe()
scaler = MinMaxScaler()
dataset_scaled = scaler.fit_transform(dataset_shifted.values)
dataset_scaled = pd.DataFrame(scaler.fit_transform(dataset_shifted),columns=dataset_shifted.columns)
dataset_scaled.index = dataset_shifted.index
dataset_scaled.head(2000)

我创建了具有相关性的热图

korelasyon = dataset_scaled.corr()
korelasyon
heatmap(korelasyon, xticklabels=dataset_shifted.columns, yticklabels=dataset_shifted.columns)

然后我尝试从数据中提取预测。这里我希望它从数据 1,2,4 中预测数据 4。

x = dataset_scaled.iloc[:,[1,2,4]]
y = dataset_scaled.iloc[:,[4]]
x
y
mlp = MLPRegressor(hidden_layer_sizes=(75,), max_iter=10000, learning_rate_init=0.05, random_state=41)
mlp.fit(x.values, y.values)
y_predicted = mlp.predict(x.values)
y_predicted
y.values.round(3)

我在从这里得到的结果中找到了 r2s 和数组。

mean_squared_error(y.values, y_predicted)
r2_score(y.values, y_predicted)
mlp.coefs_[0].round(3)
mlp.intercepts_

然后我尝试绘制我得到的数据。但是,我想在显示数据时查看横轴上的日期和纵轴上的数据。

ax= y.plot(linestyle='-')
plt.xlabel("Günler",
         fontdict={
             'family' : 'Times New Roman',
             'color' : 'black',
             'size' : 14
         })

plt.ylabel("Veriler",
         fontdict={
             'family' : 'Times New Roman',
             'color' : 'black',
             'size' : 14
         })
plt.title("Tek Katmanlı Ağır Hasta Sayısı Grafiği",
         fontdict={
             'family' : 'Times New Roman',
             'color' : 'black',
             'size' : 20
         })
plt.legend()
plt.grid()

在我在这里得到的图表中,数据在横轴上以日期的形式出现,但数据在纵轴上的比例在 0-1 之间。我想试试这个。

plt.plot(y.values.round(3)*5980, label = 'Gerçek Değer',linestyle="-", color='green')
plt.plot(y_predicted*5980, label = 'Tahmini Değer',linestyle='dashed', color='red')

plt.legend(loc="upper left")

plt.style.use(['seaborn-white'])
plt.grid()

【问题讨论】:

  • 在您的第一段中,您提到要更改日期的格式。在您帖子的最后一段中,您说 > 但是,我想在显示数据时查看横轴上的日期和纵轴上的数据。您是否尝试更改您的数据或日期?

标签: python matplotlib graph deep-learning jupyter-notebook


【解决方案1】:

其实我想要这个。我想在图表中显示日期和数据。但是数据显示为1和0。我想看看这个数据。我不想看到数字 1 和 0

【讨论】:

    猜你喜欢
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 2013-07-19
    • 2015-03-21
    • 2013-08-06
    • 1970-01-01
    相关资源
    最近更新 更多