绘制跨年熊猫的月线图

时间:2020-09-25 14:52:54

标签: python pandas dataframe matplotlib

我有一个看起来像这样的数据框:

data_T
Year    1983    1988    1995    1998    2005    2007    2010    2016    2020
Date                                    
April   1.19    -0.19   0.41    0.90    0.35    -0.22   0.48    0.99    0.45
August  -0.04   -0.99   -0.61   -1.25   -0.10   -0.77   -1.44   -0.63   -0.60
December    -0.94   -1.83   -1.00   -1.64   -0.97   -1.63   -1.65   -0.55   NaN
February    1.94    0.37    0.71    2.03    0.46    0.23    1.35    2.34    0.46
January 2.32    0.99    0.98    2.37    0.76    0.80    1.61    2.66    0.69
July    0.17    -1.45   -0.11   -0.86   -0.19   -0.55   -1.04   -0.44   -0.27
June    0.82    -1.44   -0.01   -0.24   0.12    -0.28   -0.61   0.05    -0.27
March   1.49    0.25    0.48    1.38    0.53    -0.09   0.98    1.70    0.56
May 1.18    -1.00   0.02    0.70    0.39    -0.38   -0.13   0.39    -0.19
November    -1.10   -1.85   -1.10   -1.38   -0.64   -1.58   -1.69   -0.80   NaN
October -0.96   -1.71   -0.92   -1.41   -0.10   -1.43   -1.73   -0.78   NaN
September   -0.36   -0.87   -0.90   -1.26   -0.11   -1.19   -1.67   -0.65   NaN

我创建的图每年以线图的形式绘制,x轴为月份。

现在,我想绘制第二年的下三个月(一月,二月,三月),作为向右连续的线。我将值存储在类似的数据框中。

data_T2
Year    1984    1989    1996    1999    2006    2008    2011    2017
Date                                
February    -0.18   -1.37   -0.85   -1.31   -0.57   -1.56   -1.00   0.02
January -0.69   -1.87   -0.86   -1.69   -0.81   -1.58   -1.44   -0.33
March   -0.39   -1.05   -0.56   -0.90   -0.62   -1.12   -0.83   0.12


fig, ax = plt.subplots(figsize=(12,10))


data_T.reindex(['January','February','March','April','May','June','July','August','September',
                'October', 'November', 'December']).plot(ax=ax,marker='o', color=['lightcoral','sienna','yellow','chartreuse',
                                                                             'turquoise','dodgerblue','fuchsia','purple', 'black'])
data_T2.reindex(['January','February','March']).plot(ax=ax,marker='o', color=['lightcoral','sienna','yellow','chartreuse',
                                                                             'turquoise','dodgerblue','fuchsia','purple'])
for line in ax.get_lines():
    if line._label == '2020':
        line.set_linewidth(4)
        line.set_marker('o')
ax.grid(axis='y', zorder=-1)
ax.set_ylabel("SST Anomaly (C)", weight='bold', fontsize=12)
ax.set_xlabel("Month", weight='bold', fontsize=12)
ax.xaxis.set_tick_params(labelsize=12)
ax.yaxis.set_tick_params(labelsize=12)
ax.set_title('Nino 3.4 SST Anomaly for Years Entering La Nina', fontsize=16, weight='bold')

当我将这两个图一起绘制时,由于x轴名称相似,因此它在图的左侧绘制了第二个df。我如何使绘图以一条连续线的形式继续向右移动?当前图,以进一步阐明。

enter image description here

1 个答案:

答案 0 :(得分:1)

  • 您可以按年份绘制,也可以按月份绘制
    • 如果按年份绘制,则所有线条将是首尾相连的,而不是堆叠的。
  • 我认为更好的选择是在子图中绘制相邻年份
  • 为两个图指定ylim相同,以进行缩放。
import matplotlib.pyplot as plt
import pandas as pd
import calendar

# setup data
data = {'April': {'1983': 1.19, '1988': -0.19, '1995': 0.41, '1998': 0.9, '2005': 0.35, '2007': -0.22, '2010': 0.48, '2016': 0.99, '2020': 0.45},
        'August': {'1983': -0.04, '1988': -0.99, '1995': -0.61, '1998': -1.25, '2005': -0.1, '2007': -0.77, '2010': -1.44, '2016': -0.63, '2020': -0.6},
        'December': {'1983': -0.94, '1988': -1.83, '1995': -1.0, '1998': -1.64, '2005': -0.97, '2007': -1.63, '2010': -1.65, '2016': -0.55, '2020': None},
        'February': {'1983': 1.94, '1988': 0.37, '1995': 0.71, '1998': 2.03, '2005': 0.46, '2007': 0.23, '2010': 1.35, '2016': 2.34, '2020': 0.46},
        'January': {'1983': 2.32, '1988': 0.99, '1995': 0.98, '1998': 2.37, '2005': 0.76, '2007': 0.8, '2010': 1.61, '2016': 2.66, '2020': 0.69},
        'July': {'1983': 0.17, '1988': -1.45, '1995': -0.11, '1998': -0.86, '2005': -0.19, '2007': -0.55, '2010': -1.04, '2016': -0.44, '2020': -0.27},
        'June': {'1983': 0.82, '1988': -1.44, '1995': -0.01, '1998': -0.24, '2005': 0.12, '2007': -0.28, '2010': -0.61, '2016': 0.05, '2020': -0.27},
        'March': {'1983': 1.49, '1988': 0.25, '1995': 0.48, '1998': 1.38, '2005': 0.53, '2007': -0.09, '2010': 0.98, '2016': 1.7, '2020': 0.56},
        'May': {'1983': 1.18, '1988': -1.0, '1995': 0.02, '1998': 0.7, '2005': 0.39, '2007': -0.38, '2010': -0.13, '2016': 0.39, '2020': -0.19},
        'November': {'1983': -1.1, '1988': -1.85, '1995': -1.1, '1998': -1.38, '2005': -0.64, '2007': -1.58, '2010': -1.69, '2016': -0.8, '2020': None},
        'October': {'1983': -0.96, '1988': -1.71, '1995': -0.92, '1998': -1.41, '2005': -0.1, '2007': -1.43, '2010': -1.73, '2016': -0.78, '2020': None},
        'September': {'1983': -0.36, '1988': -0.87, '1995': -0.9, '1998': -1.26, '2005': -0.11, '2007': -1.19, '2010': -1.67, '2016': -0.65, '2020': None}}
df = pd.DataFrame.from_dict(data, 'index')

data1 = {'February': {'1984': -0.18, '1989': -1.37, '1996': -0.85, '1999': -1.31, '2006': -0.57, '2008': -1.56, '2011': -1.0, '2017': 0.02},
         'January': {'1984': -0.69, '1989': -1.87, '1996': -0.86, '1999': -1.69, '2006': -0.81, '2008': -1.58, '2011': -1.44, '2017': -0.33},
         'March': {'1984': -0.39, '1989': -1.05, '1996': -0.56, '1999': -0.9, '2006': -0.62, '2008': -1.12, '2011': -0.83, '2017': 0.12}}
df1 = pd.DataFrame.from_dict(data1, 'index')

# get list of months and month abbreviations
months = list(calendar.month_name)[1:]
months_abr = list(calendar.month_abbr)[1:]

# color list
colors = ['lightcoral','sienna','yellow','chartreuse', 'turquoise','dodgerblue','fuchsia','purple', 'black']

# setup figure and plot
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(17, 8))
df.reindex(months).plot(ax=ax1, marker='o', color=colors, ylim=(-2, 3))
df1.reindex(months[:3]).plot(ax=ax2, marker='o', color=colors, ylim=(-2, 3))
ax1.set_xticks(range(12))
ax1.set_xticklabels(months_abr)
ax2.set_xticks(range(12))
ax2.set_xticklabels(months_abr)
plt.tight_layout()

enter image description here

  • 如果您得到UserWarning: FixedFormatter should only be used together with FixedLocator,则这是一个熊猫问题,显然将在v1.2.x中解决。