簡體   English   中英

我的圖例顏色與我的圖形線條顏色不匹配?

[英]My figure legend colours do not match my graph line colours?

我正在嘗試在圖形上繪制兩條線,並努力使我的圖例顏色與圖形線顏色匹配。 當我嘗試為繪圖上的線條分配顏色時,它只會更改圖例,盡管它確實會更改圖形的線條顏色,但它們也不會與圖例匹配!

這是我的代碼的基礎。

import pandas as pd
import matplotlib.pyplot as plt

df_mated = pd.read_csv("file1.txt", sep='\t', header=0)   
df_mated['Average'] = df_mated.mean(axis=1)
df_mated['SEM'] = df_mated.loc[:, :'Average'].sem()
mated_E = df_mated['SEM'].tolist()
b = df_mated['Average'].tolist()
plot1, = plt.plot(x, b, 'r-')
plt.errorbar(x, b, xerr=None, yerr=mated_E)

df_unmated = pd.read_csv("file2.txt", sep='\t', header=0) 
df_unmated['Average'] = df_unmated.mean(axis=1)
df_unmated['SEM'] = df_unmated.loc[:, :'Average'].sem()
unmated_E = df_unmated['SEM'].tolist()
c = df_unmated['Average'].tolist()
plot2, = plt.plot(x, c, 'b-')
plt.errorbar(x, c, xerr=None, yerr=unmated_E)

plt.xlabel('Position')
plt.ylabel('Average Read Depth')
plt.legend([plot1,plot2],["Mated", "Unmated"])
plt.show()

這就是我得到的: 輸出圖

如您所見,顏色不匹配,但更重要的是,配對的紅線絕對應該是圖表上的頂線。 我已經通過打印列表b和c對此進行了驗證,所以我確定。

如果刪除“ r-”和“ b-”,則會得到以下圖形: Output Graph 2

還是不對...

我是python和編碼的新手,所以如果您需要更多信息,請告訴我。 謝謝你的幫助!

PS可能您會看到我的誤差線也僅適用於一半的圖形,因為.sem()為我的熊貓數據框中的某些值生成NaN。 我認為這可能是由於除以0的錯誤,因為我的所有數據都是非常小的浮點數-但是,如果您有任何見解,也將不勝感激!

errobar線會隱藏您在圖例中顯示的線。 您可以只刪除多余的圖,而僅以相應的顏色繪制errobar(線)。 所以代替

plot1, = plt.plot(x, b, 'r-')
plt.errorbar(x, b, xerr=None, yerr=mated_E)
# ...
plot2, = plt.plot(x, c, 'b-')
plt.errorbar(x, c, xerr=None, yerr=unmated_E)

采用

plot1, _, _ = plt.errorbar(x, b, xerr=None, yerr=mated_E, color="r")
# ...
plot2, _, _ = plt.errorbar(x, c, xerr=None, yerr=unmated_E, color="b")

您基本上是在初始線條圖上繪制誤差線。 默認情況下,plt.errorbar是在每個點上都有誤差線的線圖。

# Gives a red line plot
plot1, = plt.plot(x, b, 'r-')
# Gives a '#1f77b4' (default first color) line plot with error bars
plt.errorbar(x, b, xerr=None, yerr=mated_E)

給出您的藍線。 同樣可以應用於第二個情節。

只需添加一種線型即可停用將ls=''的errobar點連接起來的線

下面的更正應該起作用:

import pandas as pd
import matplotlib.pyplot as plt

df_mated = pd.read_csv("file1.txt", sep='\t', header=0)   
df_mated['Average'] = df_mated.mean(axis=1)
df_mated['SEM'] = df_mated.loc[:, :'Average'].sem()
mated_E = df_mated['SEM'].tolist()
b = df_mated['Average'].tolist()
plot1, = plt.plot(x, b, 'r-')
# Plot only the y-errorbar, not the line connecting the datapoint
plt.errorbar(x, b, xerr=None, yerr=mated_E, ls='')

df_unmated = pd.read_csv("file2.txt", sep='\t', header=0) 
df_unmated['Average'] = df_unmated.mean(axis=1)
df_unmated['SEM'] = df_unmated.loc[:, :'Average'].sem()
unmated_E = df_unmated['SEM'].tolist()
c = df_unmated['Average'].tolist()
plot2, = plt.plot(x, c, 'b-')
# Plot only the y-errorbar, not the line connecting the datapoint
plt.errorbar(x, c, xerr=None, yerr=unmated_E, ls='')

plt.xlabel('Position')
plt.ylabel('Average Read Depth')
plt.legend([plot1,plot2],["Mated", "Unmated"])
plt.show()

調用圖並刪除plt.plot嘗試設置標簽,然后僅調用不帶參數的plt.legend()

plt.errorbar(x, b, xerr=None, yerr=mated_E, fmt='r-', label='Mated') 
plt.errorbar(x, c, xerr=None, yerr=unmated_E, fmt='b-', label='Unmated')
plt.legend()

發生的情況是顏色正確,但是隱藏在錯誤欄圖的后面。 plt.errorbar繪制線和錯誤。 由於您是在第一個圖上而不是第二個圖上設置顏色,因此最終顏色會有所不同。

至於誤差線,請檢查值是否全部相同。 在這種情況下,標准偏差將為零。

您也可以使用seaborn ,這可以節省大量時間;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM