繁体   English   中英

X标签matplotlib

[英]X labels matplotlib

我想将x轴更改为年。 年份是可变年份中的节省时间。

我想绘制如下数据: 它应该看起来像这张图片

但是,我无法用一年创建x轴。 我的情节如下图所示: 这是我的代码生成的图像的示例

我的代码如下所示:

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv("data1.csv")
demand = data["demand"]
years = data["year"]
plt.plot( demand, color='black')
plt.xlabel("Year")
plt.ylabel("Demand (GW)")
plt.show()

感谢您的任何建议。

您的示例中的plot方法不知道数据的缩放比例。 因此,为简单起见,它将demand值视为彼此分开的一个单位。 如果要用x轴表示年份,则必须告诉matplotlib它应将多少demand值视为“一年”。 如果您的数据是每月需求,则显然是每年12个值。 现在我们开始:

# setup a figure
fig, (ax1, ax2) = plt.subplots(2)

# generate some random data
data = np.random.rand(100)

# plot undesired way
ax1.plot(data)

# change the tick positions and labels ...
ax2.plot(data)

# ... to one label every 12th value
xticks = np.arange(0,100,12)

# ... start counting in the year 2000
xlabels = range(2000, 2000+len(xticks))

ax2.set_xticks(xticks)
ax2.set_xticklabels(xlabels)

plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM