Pandas 为什么twinx()创建的Axis对象的plot()方法会更改xaxis对象的格式设置程序?
我发现使用Pandas 为什么twinx()创建的Axis对象的plot()方法会更改xaxis对象的格式设置程序?,pandas,matplotlib,plot,formatter,Pandas,Matplotlib,Plot,Formatter,我发现使用twinx()方法创建的Axis对象的plot()方法在使用Datetime索引绘制Pandas系列时有一种奇怪的行为 第一种情况: import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates # Create a Formatter object fmt = mdates.DateFormatter('%Y-%m') fig = plt.figure() ax1 = f
twinx()
方法创建的Axis对象的plot()
方法在使用Datetime索引绘制Pandas系列时有一种奇怪的行为
第一种情况:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Create a Formatter object
fmt = mdates.DateFormatter('%Y-%m')
fig = plt.figure()
ax1 = fig.add_subplot(111)
# plot
data = pd.Series([1,2,3], index=pd.to_datetime(['20180101', '20180201', '20180301']))
ax1.plot(data)
# Set the major formatter of x axis
x = ax1.xaxis
x.set_major_formatter(fmt)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Create a Formatter object
fmt = mdates.DateFormatter('%Y-%m')
fig = plt.figure()
ax1 = fig.add_subplot(111)
# Set the major formatter of x axis
x = ax1.xaxis
x.set_major_formatter(fmt)
# plot
data = pd.Series([1,2,3], index=pd.to_datetime(['20180101', '20180201', '20180301']))
ax1.plot(data)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Create a Formatter object
fmt = mdates.DateFormatter('%Y-%m')
fig = plt.figure()
ax1 = fig.add_subplot(111)
# plot
data = pd.Series([1,2,3], index=pd.to_datetime(['20180101', '20180201', '20180301']))
ax1.plot(data)
# Set the major formatter of x axis
x = ax1.xaxis
x.set_major_formatter(fmt)
# Create another Axes
ax2 = ax1.twinx()
ax2.plot(data)
plt.show()
上面的代码创建一个axis对象,绘制一个使用Datetime索引的系列,然后设置x轴的主格式。我们可以通过x检查x的格式化程序是否为fmt
。获取\u major\u formatter()
第二种情况:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Create a Formatter object
fmt = mdates.DateFormatter('%Y-%m')
fig = plt.figure()
ax1 = fig.add_subplot(111)
# plot
data = pd.Series([1,2,3], index=pd.to_datetime(['20180101', '20180201', '20180301']))
ax1.plot(data)
# Set the major formatter of x axis
x = ax1.xaxis
x.set_major_formatter(fmt)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Create a Formatter object
fmt = mdates.DateFormatter('%Y-%m')
fig = plt.figure()
ax1 = fig.add_subplot(111)
# Set the major formatter of x axis
x = ax1.xaxis
x.set_major_formatter(fmt)
# plot
data = pd.Series([1,2,3], index=pd.to_datetime(['20180101', '20180201', '20180301']))
ax1.plot(data)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Create a Formatter object
fmt = mdates.DateFormatter('%Y-%m')
fig = plt.figure()
ax1 = fig.add_subplot(111)
# plot
data = pd.Series([1,2,3], index=pd.to_datetime(['20180101', '20180201', '20180301']))
ax1.plot(data)
# Set the major formatter of x axis
x = ax1.xaxis
x.set_major_formatter(fmt)
# Create another Axes
ax2 = ax1.twinx()
ax2.plot(data)
plt.show()
在本例中,我首先设置主格式化程序,然后绘制熊猫系列。使用x.get_major\u formatter()
,我们可以检查x轴的格式化程序是否仍然是fmt
。plot()方法不会更改x轴的主格式化程序
当我使用twinx()
创建另一个轴时,会发生奇怪的事情
第三种情况:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Create a Formatter object
fmt = mdates.DateFormatter('%Y-%m')
fig = plt.figure()
ax1 = fig.add_subplot(111)
# plot
data = pd.Series([1,2,3], index=pd.to_datetime(['20180101', '20180201', '20180301']))
ax1.plot(data)
# Set the major formatter of x axis
x = ax1.xaxis
x.set_major_formatter(fmt)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Create a Formatter object
fmt = mdates.DateFormatter('%Y-%m')
fig = plt.figure()
ax1 = fig.add_subplot(111)
# Set the major formatter of x axis
x = ax1.xaxis
x.set_major_formatter(fmt)
# plot
data = pd.Series([1,2,3], index=pd.to_datetime(['20180101', '20180201', '20180301']))
ax1.plot(data)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Create a Formatter object
fmt = mdates.DateFormatter('%Y-%m')
fig = plt.figure()
ax1 = fig.add_subplot(111)
# plot
data = pd.Series([1,2,3], index=pd.to_datetime(['20180101', '20180201', '20180301']))
ax1.plot(data)
# Set the major formatter of x axis
x = ax1.xaxis
x.set_major_formatter(fmt)
# Create another Axes
ax2 = ax1.twinx()
ax2.plot(data)
plt.show()
在本例中,我创建了另一个与ax1共享x轴的轴对象,然后绘制熊猫系列。如果我们使用x.get_major\u formatter()
检查x
的主格式化程序,我们可以得到PandasAutoDateFormatter
对象,而不是fmt
。ax2
的plot()
方法已更改x轴的主格式化程序
这是一个错误吗?当您使用
twinx
时,两个轴共享一个x
轴,因此,当然,如果您更改其中一个轴的格式化程序,它还必须更改other@tom当然但在第二种情况下,axis对象的plot()方法没有更改x轴的格式。但是,在第三种情况下,新Axis对象的plot()方法将其更改。这就是我所困惑的。