【问题标题】:Why are the vertical lines of plt.grid() missing in a plot from Pandas?为什么在 Pandas 的图中缺少 plt.grid() 的垂直线?
【发布时间】:2020-12-07 04:03:04
【问题描述】:

我有一个 Pandas 数据框,它有 10 个数字列,我想以 5 x 2 布局绘制所有这些列。但是,我得到的数字缺少网格的垂直线。例如,我的目标是:

df = pd.DataFrame(np.random.normal(size = (50,10)))
df.plot(subplots = True, layout = (5,2))

plt.tight_layout()
plt.show()

此代码将(正确)生成下图

现在,我的问题来了。我有以下代码:

ff_intraday_alldays_corr_df.plot(subplots = True, layout = (5,2), figsize = (20,10), sharex = False)

plt.grid(True, axis = 'both')
plt.tight_layout()
plt.show()

从这段代码中,我得到以下输出:

请注意,尽管我明确告诉pyplot 在两个轴上绘制网格,但缺少垂直线。我究竟做错了什么?我将在下面附上一个to_dict() 版本的数据框头部以便重现性。

--------- DATAFRAME 的负责人 --------

[In]: ff_intraday_alldays_corr_df.head().to_dict()

[Out]:
{'CMA_HML': {Timestamp('2020-08-17 10:00:00'): 0.44795094851406814,
  Timestamp('2020-08-17 10:05:00'): 0.38428983506941855,
  Timestamp('2020-08-17 10:10:00'): 0.41193874696647115,
  Timestamp('2020-08-17 10:15:00'): 0.4293209374846741,
  Timestamp('2020-08-17 10:20:00'): 0.43522783548870514},
 'CMA_MOM': {Timestamp('2020-08-17 10:00:00'): 0.05347989373858762,
  Timestamp('2020-08-17 10:05:00'): -0.011608270033198973,
  Timestamp('2020-08-17 10:10:00'): -0.027402429382035524,
  Timestamp('2020-08-17 10:15:00'): -0.019610957532699484,
  Timestamp('2020-08-17 10:20:00'): -0.007050143600690808},
 'CMA_RMW': {Timestamp('2020-08-17 10:00:00'): 0.20556028201595988,
  Timestamp('2020-08-17 10:05:00'): 0.09653225241133827,
  Timestamp('2020-08-17 10:10:00'): 0.11139372351072817,
  Timestamp('2020-08-17 10:15:00'): 0.11426743038168899,
  Timestamp('2020-08-17 10:20:00'): 0.11796468233334897},
 'CMA_SMB': {Timestamp('2020-08-17 10:00:00'): 0.1517609108883879,
  Timestamp('2020-08-17 10:05:00'): 0.056390472251534485,
  Timestamp('2020-08-17 10:10:00'): 0.09804054386505605,
  Timestamp('2020-08-17 10:15:00'): 0.11551034194086211,
  Timestamp('2020-08-17 10:20:00'): 0.12670084298270848},
 'HML_MOM': {Timestamp('2020-08-17 10:00:00'): -0.2978307580920829,
  Timestamp('2020-08-17 10:05:00'): -0.2560333041281664,
  Timestamp('2020-08-17 10:10:00'): -0.27203290482545234,
  Timestamp('2020-08-17 10:15:00'): -0.2702720485891392,
  Timestamp('2020-08-17 10:20:00'): -0.25899000452295107},
 'HML_RMW': {Timestamp('2020-08-17 10:00:00'): 0.17496766686559892,
  Timestamp('2020-08-17 10:05:00'): 0.05867960946753955,
  Timestamp('2020-08-17 10:10:00'): 0.07492619768022696,
  Timestamp('2020-08-17 10:15:00'): 0.07544496840758017,
  Timestamp('2020-08-17 10:20:00'): 0.07378940456925964},
 'HML_SMB': {Timestamp('2020-08-17 10:00:00'): 0.11783436515538953,
  Timestamp('2020-08-17 10:05:00'): 0.0572659935968401,
  Timestamp('2020-08-17 10:10:00'): 0.10589632150389904,
  Timestamp('2020-08-17 10:15:00'): 0.13172904632035068,
  Timestamp('2020-08-17 10:20:00'): 0.15248571968608735},
 'MOM_RMW': {Timestamp('2020-08-17 10:00:00'): 0.09347437113499303,
  Timestamp('2020-08-17 10:05:00'): 0.10238296372223599,
  Timestamp('2020-08-17 10:10:00'): 0.09467287450167983,
  Timestamp('2020-08-17 10:15:00'): 0.08173228399896171,
  Timestamp('2020-08-17 10:20:00'): 0.08894348736883101},
 'MOM_SMB': {Timestamp('2020-08-17 10:00:00'): 0.11279955572946082,
  Timestamp('2020-08-17 10:05:00'): -0.01565427697073142,
  Timestamp('2020-08-17 10:10:00'): -0.019023170042160316,
  Timestamp('2020-08-17 10:15:00'): -0.0055759975445638255,
  Timestamp('2020-08-17 10:20:00'): -0.0025691959978936586},
 'RMW_SMB': {Timestamp('2020-08-17 10:00:00'): -0.020075628073247538,
  Timestamp('2020-08-17 10:05:00'): -0.16367953534291818,
  Timestamp('2020-08-17 10:10:00'): -0.13591209255538989,
  Timestamp('2020-08-17 10:15:00'): -0.14175163614235164,
  Timestamp('2020-08-17 10:20:00'): -0.14258055578226198}}

【问题讨论】:

    标签: python pandas matplotlib plot


    【解决方案1】:

    这是我使用 Plotly 的解决方案:

    # Import packages
    from plotly.subplots import make_subplots
    import plotly.graph_objects as go
    import pandas as pd
    import numpy as np
    
    # Create data table (replace with yours)
    df = pd.DataFrame(np.random.normal(size = (50,10)))
    
    # Initialize plotly subplots with shared x axis
    rows = 5
    cols = 2
    fig = make_subplots(rows=rows, cols=cols, shared_xaxes=True,)
    
    # Loop through subplots and add data to them
    counter = 0
    for i in range(rows):
        for j in range(cols):
            fig.add_trace(
                go.Scatter(x=np.arange(0,50), y=df.iloc[:,counter], name=counter),
                row=(i+1), col=(j+1)
            )
            counter += 1
            
    # Configure plot and show it        
    fig.update_layout(height=800, width=800, title_text="Plotly Subplots with Custom Grid Lines")
    fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='black')
    fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='black')
    fig.show()
    

    具有以下输出:

    如果您更喜欢 matplotlib 以获得更轻量级的输出,这里是相同的代码:

    df = pd.DataFrame(np.random.normal(size = (50,10)))
    
    rows = 5
    cols = 2
    fig, axs = plt.subplots(rows, cols, figsize=(10,10))
    
    counter = 0
    for i in range(rows):
        for j in range(cols):
            axs[i, j].plot(df.iloc[:,counter], label=counter)
            axs[i, j].grid()
            axs[i, j].legend(loc='upper right')
            counter += 1
    

    Plotly 绘图的好处是您可以放大各个区域,控制 x/y 轴边界,并在将鼠标悬停在这些区域上时查看准确的数据值!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      • 2020-08-11
      • 2018-02-15
      • 2012-04-28
      • 2022-09-24
      • 2020-08-22
      • 1970-01-01
      相关资源
      最近更新 更多