2 回答

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊
繪制此圖的更好方法是使用面向?qū)ο蟮?matplotlib api。首先,我們必須定義我們的Figure,axes然后為了正確繪制第二個(gè) y,我們將創(chuàng)建一個(gè)偽軸對(duì)象,該對(duì)象鏈接回我們創(chuàng)建的原始軸。然后我們可以告訴 pandas 直接在我們的軸上繪圖,以確保所有內(nèi)容都到達(dá)正確的位置。
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np
import pandas as pd
import seaborn as sns
flatui1 = ["#0C6514", "#18AB25"]
flatui2 = ["#0E1D56", "#18AB25"]
colors = sns.color_palette(flatui1)
cmap1 = LinearSegmentedColormap.from_list("my_colormap", colors)
colors = sns.color_palette(flatui2)
cmap2 = LinearSegmentedColormap.from_list("my_colormap", colors)
sns.set_style(style='white') # we don't want the grid coming from seaborn
m1_t = pd.DataFrame({
? ? "A":[0.21,0.05,1.22,0.41,1.28,1.15,0.91,0.63,0.38,1.18],
? ? "B":[13.33,18,23.69,21.46,35.31,16,20.11,15.87,20.53,17.71],
? ? "C":[5.71,2,23.44,9.02,35.39,13.48,14.62,13.17,13.68,14.66]
})
fig, ax = plt.subplots()
twin_x = ax.twinx() # Create a pseudo axes based off of the original
# ax is our main plot with the "primary y-axis"
# twin_x is also our main plot, but plotting on this plots
#? ?our "secondary y" axis
# Put the bar plot on the "primary y" via ax=ax
m1_t['A'].plot(kind='bar',colormap=cmap1, ax=ax, zorder=1)
# Put the line plot on the "secondary y" via ax=twin_x
#? don't have pandas place our legend by default, we'll do this manually for more control later
m1_t[['B','C']].plot(kind='line', colormap=cmap2, ax=twin_x, zorder=2, legend=False)
ax.grid(True, zorder=0)
ax.set_axisbelow(True)
ax.set_xticklabels(('P0', 'P1','P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9'))
# to keep the line and bar legends separate:
#? you can simply draw a legend on each one, since each
#? respective Axes holds onto its own data/artists
ax.legend(loc="upper left")
twin_x.legend(loc="upper left", bbox_to_anchor=(0, .85))
?
# To create 1 all encompassing legend:
#? you can use fig.legend with some tweaking
#? fig.legend automatically gathers legend information from all Axes on the figure
#? we'll need to give it a bounding box, as well as a new coordinate system so
#? that it will appear inside of the bounds of the Axes (instead of the bounds of the figure)
fig.legend(bbox_to_anchor=(.9, 1), bbox_transform=ax.transAxes)
# Legends on the left are the legends we made with ax.legend(...) + twin_x.legend(...)
# legend on the right is the all encompassing fig.legend(...)
plt.show()
無論代碼行的順序如何,該解決方案都將起作用,因?yàn)槲覀兏嬖V pandas 在特定軸上繪制,而不是讓它選擇在一組現(xiàn)有軸上繪制或創(chuàng)建一個(gè)新軸。
編輯:
手動(dòng)指定 zorder 是控制元素繪制順序的可靠方法。本質(zhì)上,具有較高 zorder 的元素將位于具有較低 zorder 的元素之上。在本例中,我們的網(wǎng)格的 zorder 為 0,條形圖和線條的 zorder 為 1 和 2,確保它們將放置在網(wǎng)格的頂部(因?yàn)樗鼈兊?zorder 高于 0)。
編輯2(添加圖例):
左邊的圖例是我們用 ax.legend(...) + twin_x.legend(...) 創(chuàng)建的圖例
右側(cè)的圖例是無所不包的Fig.legend(...) 有關(guān)方法的描述,請(qǐng)參閱代碼中的注釋

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊
以下兩軸圖方法很簡(jiǎn)單,因?yàn)樗A羲饕瑘D例。
# This two line sequence has the problem
# m1_t['A'].plot(kind='bar',colormap=cmap1)
# m1_t[['B','C']].plot(kind='line',secondary_y=True,colormap=cmap2)
ax = m1_t.plot(y='A', kind='bar',colormap=cmap1)
ax1 = m1_t.plot(y=['B','C'], kind='line',secondary_y=True,colormap=cmap2, ax=ax)
- 2 回答
- 0 關(guān)注
- 132 瀏覽
添加回答
舉報(bào)