亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

矩陣 cmap 顏色的順序不同

矩陣 cmap 顏色的順序不同

ABOUTYOU 2022-09-13 17:18:37
我在同一圖上繪制了兩個數據幀,以及每個數據集具有相同cmap的每個數據集的均值。但是,將 cmap 應用于不同數據集的顏色順序是不同的。有人可以指出我做錯了什么嗎?這是輸出。如您所見,標記具有相同的形狀,但顏色不同(x):代碼如下:import matplotlibimport pandas as pdimport matplotlib.pyplot as pltimport numpy as np#readingdf1 = pd.DataFrame(np.random.randint(10,20,size=(7, 8)), columns=list('ABCDEFGH'))df2 = pd.DataFrame(np.random.randint(30,40,size=(7, 8)), columns=list('ABCDEFGH'))#plot the first DataFrame#trying to select the first 8 RGBA codes from viridis - does not woax = df1.plot(style = ['.','*','1','2','3','4','+','x'],figsize=(8,4),cmap = 'Accent')#ax.set_prop_cycle(cycler(color = cmap.colors[0:7]))ax = df1.mean(axis=1).plot(c='red',style = '--',label = 'M1 mean')#plot the second dataframeax = df2.mean(axis=1).plot(ax=ax,c='black',style = '--',label = 'M3 mean')ax = df2.plot(ax=ax,style = ['.','*','1','2','3','4','+','x'],cmap = 'Accent')#fiddle with the axesplt.ylim(0,40)plt.xlim(-0.5,6.2)#add the labelsplt.ylabel('Average Efficiency')#make sure all the ticks are visibleplt.xticks(np.arange(0,7),np.arange(0,7))plt.xticks([0,1,2,3,4,5,6],['Mon','Tue','Wed','Thu','Fri','Sat','Sun'])#change the legendplt.legend([1,2,3,4,5,6,7,8,'M1_mean','M3 mean'],title = 'Groups',bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.1)#increase the fontfont = {'family' : 'normal',        'weight' : 'normal',        'size'   : 10}matplotlib.rc('font', **font)plt.show()print('done')
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

奇怪的事情正在發生,可能是熊貓與matplotlib一起工作的某種錯誤:每當標記是小寫字母時,它似乎并不服從給定的色彩映射表,它只是遵循“prop_cycle”。

以下是兩種解決方法。最簡單的方法是避免所有這些小寫字母標記并選擇不同的標記。

另一種解決方法是顯式設置顏色周期,并在繪制第二部分時重置它。請注意,從 viridis 色彩映射表中選擇 9 種間距相等的顏色。如果沒有設置明確的數字,viridis有256種顏色,其中8種顏色都非常相似(深紫色)。我們選擇9種顏色,后來忽略最后一種顏色,因為黃色對于此應用程序來說對比度太小。(別忘了省略大熊貓的論點)。cmap = plt.cm.get_cmap('viridis', 9)cmapplot

顯式顏色循環可以更好地控制使用哪些顏色。您還可以選擇例如cmap = plt.cm.get_cmap(“Dark2”),它只有較暗的顏色,并且與白色背景具有足夠的對比度。

下面是一些代碼來演示它是如何工作的:

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import matplotlib


df1 = pd.DataFrame(np.random.randint(10, 25, size=(7, 8)), columns=list('ABCDEFGH'))

df2 = pd.DataFrame(np.random.randint(30, 45, size=(7, 8)), columns=list('ABCDEFGH'))


# create a color map with 9 colors from viridis

cmap = plt.cm.get_cmap('viridis', 9)


fig, ax = plt.subplots(figsize=(8, 4))

markers = ['.', '*', '1', '2', '3', '4', '+', 'x']

# plot the first dataframe

# set the prop_cycle to use 8 colors from the given colormap

ax.set_prop_cycle(color=cmap.colors[0:8])

df1.plot(style=markers, ax=ax)

df1.mean(axis=1).plot(c='red', style='--', label='M1 mean', ax=ax)


# plot the second dataframe

ax.set_prop_cycle(color=cmap.colors[0:8])

df2.plot(ax=ax, style=markers)

df2.mean(axis=1).plot(ax=ax, c='black', style='--', label='M3 mean')


plt.ylim(0, 45)

plt.xlim(-0.5, 6.2)

plt.ylabel('Average Efficiency')

plt.xticks(range(7), ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'])

# change the legend

plt.legend(title='Groups', bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.1, ncol=2)


font = {'family': 'normal',

        'weight': 'normal',

        'size': 10}

matplotlib.rc('font', **font)

plt.tight_layout()

plt.show()

http://img1.sycdn.imooc.com//63204b3300012d8f23230687.jpg

查看完整回答
反對 回復 2022-09-13
  • 1 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號