我有以下 Matplotlib 圖
我想知道是否可以在 Matplotlib 中的 x 軸上包含其他資訊作為我在情節的開頭和結尾包含的紅色日期。
在我當前的版本中,這些紅色日期不在情節中。我使用以下當前代碼來創建繪圖:
plt.title('Test Forecast (Best)')
plt.plot(forecast, zorder=1)
plt.plot(actual, zorder=0)
plt.xlabel('Timeslot')
plt.ylabel('Load')
plt.legend(['forecast', 'actual'], loc='upper left')
filename = folderPath "\Forecast_Test_Best.png"
plt.savefig(filename, bbox_inches='tight', dpi=200)
plt.show()
更新:我試圖從答案中包含建議,我有這個代碼:
plt.title('Test Forecast (Best)')
plt.plot(forecast, zorder=1)
plt.plot(actual, zorder=0)
plt.xlabel('Timeslot')
plt.ylabel('Load')
plt.legend(['forecast', 'actual'], loc='upper left')
fig, ax = plt.subplots()
ax.plot(np.arange(40))
# the x coords of this transformation are data, and the y coord are axes
trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes)
ax.text(0, -0.07, 'LHS annotate', transform=trans, va='top', ha='center', color='r')
plt.savefig(filename, bbox_inches='tight', dpi=200)
plt.show()
但現在我得到了 2 個圖:沒有注釋的原始帖子和一個帶有注釋的只有線性函式的新圖(我不想繪制,我不知道它來自哪里)。我想要的是在我的圖中有注釋。
uj5u.com熱心網友回復:
有很多方法可以做到這一點,但也許最有用的是了解一點關于變換堆疊的知識:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.transforms as mtransforms
fig, ax = plt.subplots()
ax.plot(np.arange(40))
# the x coords of this transformation are data, and the y coord are axes
trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes)
ax.text(0, -0.07, 'LHS annotate', transform=trans, va='top', ha='center', color='r')
plt.show()

請注意,這里我們為 x 指定了資料轉換,為 y 指定了相對于軸的單位。
有人可能會抱怨 0.07 的 fudge-factor 不是很好,所以我們可以應用物理單位的偏移變換:
trans = mtransforms.ScaledTranslation(0, -20/72, fig.dpi_scale_trans)
ax.text(0, -0.0, 'LHS annotate', transform=trans, va='top', ha='center', color='r')
偏移量現在以點為單位:

有關轉換的更多資訊,請參閱:https : //matplotlib.org/stable/tutorials/advanced/transforms_tutorial.html#using-offset-transforms-to-create-a-shadow-effect
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383666.html
標籤:Python matplotlib
