为什么Pandas的绘图中缺少plt.grid()的垂直线?

2024-05-19 03:39:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Pandas数据框,它有10个数字列,我想用5×2的布局来绘制它们。然而,我得到的图形缺少网格的垂直线。例如,我的目标是:

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

plt.tight_layout()
plt.show()

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

enter image description here

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

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()

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

enter image description here

请注意,垂直线丢失,尽管我明确告诉pyplot在两个轴上绘制网格。我做错了什么?我将在下面附上数据帧头部的to_dict()版本,以确保再现性

------------数据帧头--------

[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}}

Tags: 数据代码true网格df绘制plttimestamp
1条回答
网友
1楼 · 发布于 2024-05-19 03:39:05

以下是我使用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()

具有以下输出: Plotly subplots

如果您更喜欢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 plots的好处在于,您可以放大各种区域,控制x/y轴边界,并在将鼠标悬停在这些区域上时查看精确的数据值

相关问题 更多 >

    热门问题