如何减少Xlims的数量?

时间:2019-06-10 04:34:03

标签: python pandas datetime matplotlib

我想减少xlim标签,因为我使用的是日期时间信息,并且占用xlim的空间很大。问题是我想读的时候

我想我需要一些缩放比例

dates = pd.read_csv("EURUSDtest.csv")
dates = dates["Date"]+" " + dates["Time"]
plt.title("EUR/USD")
plt.plot(dates, data_pred)
plt.xticks(rotation="vertical")
plt.tick_params(labelsize=10)
plt.plot(forecasting)

问题...

enter image description here

1 个答案:

答案 0 :(得分:0)

IIUC:您需要通过调用pd.to_datetime将dates列转换为pandas datetime类型。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# To reproduce the issue you have lets create a date column as string
df = pd.DataFrame({"Dates":pd.date_range(start='2018-1-1', end='2019-1-1', freq='15MIN').strftime("%m-%d-%Y %H-%M-%S")})
# Convert the date string to date type 
df["Dates"] = pd.to_datetime(df["Dates"])
# Add column to assign some dummy values
df = df.assign(VAL=np.linspace(10, 110, len(df)))
# Plot the graph
# Now the graph automatically adjusts the XLIM based on the size of the graph
plt.title("eur/usd")
plt.plot(df["Dates"], df["VAL"])
plt.xticks(rotation="vertical")
plt.show()

但是,如果您需要根据需要进一步控制xlim,则需要阅读matplotlib教程。

enter image description here