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

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

matplotlib 條形圖僅突出顯示值

matplotlib 條形圖僅突出顯示值

泛舟湖上清波郎朗 2021-09-14 10:05:45
嗨,我想得到這種條形圖。問(wèn)題是如何通過(guò)選擇設(shè)置相應(yīng)的xlables?我編碼如下以刪除不需要的國(guó)家/地區(qū)標(biāo)簽,但圖表也有 nan 作為標(biāo)簽。countries=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy']new_index=list(df.index)for i in range(len(new_index)):    if new_index[i] not in countries :        new_index[i]=np.nan這是我的結(jié)果,標(biāo)簽中為 nan,條形之間的距離更寬:對(duì)于數(shù)據(jù):import numpy as npimport pandas as pd#Overall Country listCountries=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy','Czech Republic', 'Austria', 'Slovak Republic', 'Slovenia', 'Germany', 'Portugal', 'Hungary', 'Colombia', 'New Zealand', 'Norway', 'Latvia']#Countries to highlightDesired=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy']np.random.seed(0)Value=np.random.rand(len(Countries))df = pd.DataFrame({'Countries': Countries,'Value': Value,})df.sort_values(['Value'],inplace=True)df.set_index('Countries',drop=True,inplace=True)ax_1 = df['Value'].plot(kind='bar', title ="graph", figsize=(10, 6), fontsize=12)ax_1.set_xlabel("Country Name", fontsize=12)plt.show()
查看完整描述

1 回答

?
www說(shuō)

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊

運(yùn)行 x-ticks,然后根據(jù)countries列表禁用其中的幾個(gè)。


import numpy as np

import pandas as pd


#Overall Country list

Countries=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy','Czech Republic',

 'Austria',

 'Slovak Republic',

 'Slovenia',

 'Germany',

 'Portugal',

 'Hungary',

 'Colombia',

 'New Zealand',

 'Norway',

 'Latvia']


#Countries to highlight

Desired=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy']


np.random.seed(0)

Value=np.random.rand(len(Countries))

df = pd.DataFrame({'Countries': Countries,'Value': Value,})

df.sort_values(['Value'],inplace=True)


df.set_index('Countries',drop=True,inplace=True)

ax_1 = df['Value'].plot(kind='bar', title ="graph", figsize=(10, 6), fontsize=12)

ax_1.set_xlabel("Country Name", fontsize=12)




for ticks in ax_1.xaxis.get_major_ticks():

    if ticks.label1.get_text() not in Desired:

        ticks.label1.set_visible(False)

        ax_1.patches[df.index.get_indexer([ticks.label1.get_text()])[0]].set_facecolor('w')

        ax_1.patches[df.index.get_indexer([ticks.label1.get_text()])[0]].set_edgecolor('black')

    else:

        ax_1.patches[df.index.get_indexer([ticks.label1.get_text()])[0]].set_facecolor('r')

        

http://img1.sycdn.imooc.com//614003b8000198a507760573.jpg

查看完整回答
反對(duì) 回復(fù) 2021-09-14
  • 1 回答
  • 0 關(guān)注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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