第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

條形圖(主要)然后折線圖(次要)工作正常,但如果我更改代碼中的順序則不起作用

條形圖(主要)然后折線圖(次要)工作正常,但如果我更改代碼中的順序則不起作用

PHP
翻閱古今 2023-11-09 17:07:44
我需要將這兩個(gè)圖放在一起,但是當(dāng)我使用條形圖(主要)然后使用折線圖(次要)時(shí),它工作得很好。如果我改變關(guān)于情節(jié)的代碼行中的順序,它就不起作用。import matplotlib.pyplot as pltimport numpy as npimport pandas as pdflatui1 = ["#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='whitegrid')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]})# This two line sequence has the problemm1_t['A'].plot(kind='bar',colormap=cmap1)m1_t[['B','C']].plot(kind='line',secondary_y=True,colormap=cmap2)ax = plt.gca()ax.grid(True)ax.set_axisbelow(True)ax.set_xticklabels(('P0', 'P1','P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9'))plt.savefig('Comparison',dpi=300)plt.show()https://i.stack.imgur.com/I9yXy.png
查看完整描述

2 回答

?
飲歌長(zhǎng)嘯

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)參閱代碼中的注釋


查看完整回答
反對(duì) 回復(fù) 2023-11-09
?
忽然笑

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)


查看完整回答
反對(duì) 回復(fù) 2023-11-09
  • 2 回答
  • 0 關(guān)注
  • 132 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)