Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python区域绘图:剪切日期x记号位置和标签,以及设置的x限制_Python_Pandas_Matplotlib_Date Range_Xticks - Fatal编程技术网

Python区域绘图:剪切日期x记号位置和标签,以及设置的x限制

Python区域绘图:剪切日期x记号位置和标签,以及设置的x限制,python,pandas,matplotlib,date-range,xticks,Python,Pandas,Matplotlib,Date Range,Xticks,我必须输入以下代码: import numpy as np import pandas as pd import matplotlib.pyplot as plt #Area Plot plt.figure(figsize=(15,5)) x=pd.date_range('1992-1-1','2014-12-31',freq='6MS').strftime("%Y-%m").tolist() y=np.random.uniform(-3, 3 , len(x)) plt.

我必须输入以下代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#Area Plot
plt.figure(figsize=(15,5))
x=pd.date_range('1992-1-1','2014-12-31',freq='6MS').strftime("%Y-%m").tolist()
y=np.random.uniform(-3, 3 , len(x))
plt.fill_between(x[1:], y[1:], 0, where=y[1:] >= 0, facecolor='red', interpolate=True, alpha=0.7,label='Up')
plt.fill_between(x[1:], y[1:], 0, where=y[1:] <= 0, facecolor='green', interpolate=True, alpha=0.7,label='Down')
plt.show
#plt.savefig('file')
当我试图更改XTick的标签时,绘图变为空,标签消失

plt.xticks(["1990","1995","2000","2005","2010","2015"])
它无法按预期工作,标签已移动。我有三个问题:

(1) 如何每年或五年显示x标签
(2) 如何每6个月或每年显示x记号

(3) 如何将xlim从1993年设置到2015年?

处理此问题的一种方法是通过

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#Area Plot
plt.figure(figsize=(15,5))
x=pd.date_range('1992-1-1','2014-12-31',freq='6MS').strftime("%Y-%m").tolist()
y=np.random.uniform(-3, 3 , len(x))
plt.fill_between(x[1:], y[1:], 0, where=y[1:] >= 0, facecolor='red', interpolate=True, alpha=0.7,label='Up')
plt.fill_between(x[1:], y[1:], 0, where=y[1:] <= 0, facecolor='green', interpolate=True, alpha=0.7,label='Down')
plt.xticks(x, rotation=90)
plt.show()

此代码有效

import numpy as np
import pandas as pd
from datetime import datetime, date, time
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

x=pd.date_range('1993-01-01', periods=44, freq='2Q',closed='left')
y=np.random.uniform(-3, 3 , 44)


fig=plt.figure(figsize=(15,5))
ax=fig.add_subplot(1,1,1)

ax.fill_between(x[0:], y[0:], 0, where=y[0:] >= 0, facecolor='red', interpolate=True, alpha=0.7,label='Up')
ax.fill_between(x[0:], y[0:], 0, where=y[0:] <= 0, facecolor='green', interpolate=True, alpha=0.7,label='Down')

#[Questions 1 and 2] format the x-ticks and labels
years = mdates.YearLocator()   # every 1 year
#years = mdates.YearLocator(5)   # every 5 years

months = mdates.MonthLocator(bymonth=[1,7,13])  # every month
years_fmt = mdates.DateFormatter('%Y')
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(years_fmt)
ax.xaxis.set_minor_locator(months)

#[Question 3]x-axis limit
Start=1993
End=2015
start = datetime(year=Start, month=1, day=1, hour=0)
end   = datetime(year=End, month=1, day=1, hour=0)
ax.set_xlim(start,end)

plt.show()
将numpy导入为np
作为pd进口熊猫
从日期时间导入日期时间、日期、时间
将matplotlib.pyplot作为plt导入
将matplotlib.dates导入为mdates
x=pd.日期范围('1993-01-01',周期=44,频率=2Q',关闭=left')
y=np.随机均匀(-3,3,44)
图=plt.图(图尺寸=(15,5))
ax=图add_子批次(1,1,1)
ax.fill_介于(x[0:],y[0:],0,其中=y[0:]>=0,facecolor='red',interpolate=True,alpha=0.7,label='Up')
ax.fill_介于(x[0:],y[0:],0,其中=y[0:]
import numpy as np
import pandas as pd
from datetime import datetime, date, time
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

x=pd.date_range('1993-01-01', periods=44, freq='2Q',closed='left')
y=np.random.uniform(-3, 3 , 44)


fig=plt.figure(figsize=(15,5))
ax=fig.add_subplot(1,1,1)

ax.fill_between(x[0:], y[0:], 0, where=y[0:] >= 0, facecolor='red', interpolate=True, alpha=0.7,label='Up')
ax.fill_between(x[0:], y[0:], 0, where=y[0:] <= 0, facecolor='green', interpolate=True, alpha=0.7,label='Down')

#[Questions 1 and 2] format the x-ticks and labels
years = mdates.YearLocator()   # every 1 year
#years = mdates.YearLocator(5)   # every 5 years

months = mdates.MonthLocator(bymonth=[1,7,13])  # every month
years_fmt = mdates.DateFormatter('%Y')
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(years_fmt)
ax.xaxis.set_minor_locator(months)

#[Question 3]x-axis limit
Start=1993
End=2015
start = datetime(year=Start, month=1, day=1, hour=0)
end   = datetime(year=End, month=1, day=1, hour=0)
ax.set_xlim(start,end)

plt.show()