简体   繁体   English

X轴比例尺与同一绘图上的2个数据集不匹配

[英]X-Axis scales not matching with 2 data sets on same plot

I have 2 datasets that I'm trying to plot on the same figure. 我有2个试图在同一图上绘制的数据集。 They share a common column that I'm using for the X-axis, however one of my sets of data is collected annually and the other monthly so the number of data points in each set is significantly different. 它们共享一个我在X轴上使用的公用列,但是我的一组数据是每年收集的,而另一组是每月收集的,因此每组数据点的数量有很大不同。

Pyplot is not plotting the X values for each set where I would expect when I plot both sets on the same graph 当我在同一张图上绘制两个集合时,Pyplot没有绘制每个集合的X值

When I plot just my annually collected data set I get: 当只绘制年度收集的数据集时,我得到: 税务资料

When I plot just my monthly collected data set I get: 当我只绘制每月收集的数据集时,我得到: 行程数据

But when I plot the two sets overlayed (code below) I get: 但是当我绘制覆盖的两个集合(下面的代码)时,我得到了: 两者数据

tframe: TFRAME:

   10003   Date
0    257 201401
1    216 201402
2    417 201403
3    568 201404
4    768 201405
5    836 201406
6    798 201407
7    809 201408
8    839 201409
9    796 201410

tax_for_zip_data: tax_for_zip_data:

TAX BRACKET $1 under $25,000    ...       Date
2                       5740    ...     201301
0                       5380    ...     201401
1                       5320    ...     201501
3                       5030    ...     201601

So I did as wwii suggested in the comments and converted my Date columns to datetime objects: 因此,我按照wwii在注释中的建议进行了操作,并将Date列转换为datetime对象:

tframe: TFRAME:

   10003       Date
0    257 2014-01-31
1    216 2014-02-28
2    417 2014-03-31
3    568 2014-04-30
4    768 2014-05-31
5    836 2014-06-30
6    798 2014-07-31
7    809 2014-08-31
8    839 2014-09-30
9    796 2014-10-31

tax_for_zip_data: tax_for_zip_data:

TAX BRACKET $1 under $25,000    ...           Date
2                       5740    ...     2013-01-31
0                       5380    ...     2014-01-31
1                       5320    ...     2015-01-31
3                       5030    ...     2016-01-31

But the dates are still plotting offset, 但是日期仍然在画偏移量, 两者数据

None of my data goes back to 2012- Jan 2013 is the earliest. 我的数据都无法追溯到2012年-2013年1月是最早的数据。 The tax_for_zip_data are all offset by a year. tax_for_zip_data全部抵消了一年。 If I plot just that set alone it plots properly. 如果我仅绘制该图集,则可以正确绘制。 税务资料

fig, ax1 = plt.subplots(sharex = True)

color = "tab:red"
ax1.set_xlabel('Date')
ax1.set_ylabel('Trips', color = color)
tframe.plot(kind = 'line',x = 'Date', y = "10003", ax = ax1, color = color)
ax1.tick_params(axis = 'y', labelcolor = color)


ax2 = ax1.twinx()
color = "tab:blue"
ax2.set_ylabel('Num Returns', color = color)
tax_for_zip_data.plot(kind = 'line', x = 'Date', y = tax_for_zip_data.columns[:-1], ax = ax2)
ax2.tick_params(axis = 'y', labelcolor = color)

plt.show()

If you can make the DataFrame index a datetime index plotting is easier. 如果可以使DataFrame索引更容易进行日期时间索引的绘制。

s = '''10003   Date
257   201401
216   201402
417   201403
568   201404
768   201405
836   201406
798   201407
809   201408
839   201409
796   201410
'''
df1 = pd.read_csv(io.StringIO(s), delimiter='\s{2,}',engine='python')
df1.index = pd.to_datetime(df1['Date'],format='%Y%m')

s = '''TAX BRACKET     $1 under $25,000           Date
2                       5740         201301
0                       5380         201401
1                       5320         201501
3                       5030         201601
'''
df2 = pd.read_csv(io.StringIO(s), delimiter='\s{2,}',engine='python')
df2.index = pd.to_datetime(df2['Date'],format='%Y%m')

You don't need to specify an argument for plot 's x parameter. 您无需为plotx参数指定参数。

fig, ax1 = plt.subplots(sharex = True)

color = "tab:red"
ax1.set_xlabel('Date')
ax1.set_ylabel('Trips', color = color)
df1.plot(kind = 'line',y="10003", ax = ax1, color = color)
ax1.tick_params(axis = 'y', labelcolor = color)

ax2 = ax1.twinx()
color = "tab:blue"
ax2.set_ylabel('Num Returns', color = color)
df2.plot(kind = 'line', y='$1 under $25,000', ax = ax2)
ax2.tick_params(axis = 'y', labelcolor = color)

plt.show()
plt.close()

在此处输入图片说明

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

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