为什么轮廓图的一部分显示为白色?

时间:2020-04-30 00:55:01

标签: python matplotlib contourf

我正在使用Python的library(tidyverse) df <- structure( list(ID = c("P-1", " P-1", "P-1", "P-2", "P-3", "P-4", "P-5", "P-6", "P-7", "P-8"), Date = c("2020-03-16 12:11:33", "2020-03-16 13:16:04", "2020-03-16 06:13:55", "2020-03-16 10:03:43", "2020-03-16 12:37:09", "2020-03-16 06:40:24", "2020-03-16 09:46:45", "2020-03-16 12:07:44", "2020-03-16 14:09:51", "2020-03-16 09:19:23"), Status = c("SA", "SA", "SA", "RE", "RE", "RE", "RE", "XA", "XA", "XA"), Flag = c("L", "L", "L", NA, "K", "J", NA, NA, "H", "G"), Value = c(5929.81, 5929.81, 5929.81, NA, 6969.33, 740.08, NA, NA, 1524.8, NA), Flag2 = c("CL", "CL", "CL", NA, "RY", "", NA, NA, "", NA), Flag3 = c(NA, NA, NA, NA, "RI", "PO", NA, "SS", "DDP", NA)), .Names=c("ID", "Date", "Status", "Flag", "Value", "Flag2", "Flag3"), row.names=c(NA, 10L), class="data.frame") df2 <- df %>% mutate( # add variables Value = ifelse(0 <= Value & Value <= 15000, "0-15000", "15000-50000"), substatus = case_when( !is.na(Flag2) & is.na(Flag3) ~ "a", !is.na(Flag3) & is.na(Flag2) ~ "b", !is.na(Flag3) & !is.na(Flag2) ~ "c", TRUE ~ "d"), # make Date an actual date rather than a timestamp Date = as.Date(Date), # remove obsolete columns Flag2 = NULL, Flag3 = NULL, ID = NULL, # renames NAs into the name of the desired column Flag = ifelse(is.na(Flag), "[Null]", Flag), # create column of 1 for pivot temp = 1, # and row id id = row_number() ) %>% # create new columns L K etc, this also drops the Flag col pivot_wider(names_from = "Flag", values_from = "temp", values_fill = list(temp=0)) %>% # move `[Null]` column to the end select(everything(), -`[Null]`, `[Null]`) %>% mutate( id = NULL, count = 1, Total = rowSums(select(., L:`[Null]`))) df2 #> # A tibble: 10 x 12 #> Date Status Value substatus L K J H G `[Null]` #> <date> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 2020-03-16 SA 0-15~ a 1 0 0 0 0 0 #> 2 2020-03-16 SA 0-15~ a 1 0 0 0 0 0 #> 3 2020-03-16 SA 0-15~ a 1 0 0 0 0 0 #> 4 2020-03-16 RE <NA> d 0 0 0 0 0 1 #> 5 2020-03-16 RE 0-15~ c 0 1 0 0 0 0 #> 6 2020-03-16 RE 0-15~ c 0 0 1 0 0 0 #> 7 2020-03-16 RE <NA> d 0 0 0 0 0 1 #> 8 2020-03-16 XA <NA> b 0 0 0 0 0 1 #> 9 2020-03-16 XA 0-15~ c 0 0 0 1 0 0 #> 10 2020-03-16 XA <NA> d 0 0 0 0 1 0 #> # ... with 2 more variables: count <dbl>, Total <dbl> # As you didn't tell what to do with NA values so I left them as NA bind_rows( df2 %>% # add missing combinations of abcd complete(nesting(Date, Status, Value), substatus) %>% group_by(Date, Value, Status, substatus) %>% summarize_all(~sum(., na.rm=TRUE)) %>% group_by(Status, Value) %>% mutate(percent = paste(round(100 * Total / sum(Total), 2), "%")) %>% ungroup(), df2 %>% mutate(substatus = Status, Status = paste0(Status, "_")) %>% group_by(Date, Value, Status, substatus) %>% mutate(count = n()) %>% group_by(count, add = TRUE) %>% summarize_all(~sum(., na.rm=TRUE)) %>% group_by(Value) %>% mutate(percent = paste(round(100 * Total / sum(Total), 2), "%")) ) %>% arrange(Date, Value, desc(Status)) %>% mutate(Status = NULL) %>% rename(Status = substatus) %>% print(n=Inf) #> # A tibble: 25 x 12 #> Date Value Status L K J H G `[Null]` count Total #> <date> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 2020-03-16 0-15~ XA 0 0 0 1 0 0 1 1 #> 2 2020-03-16 0-15~ a 0 0 0 0 0 0 0 0 #> 3 2020-03-16 0-15~ b 0 0 0 0 0 0 0 0 #> 4 2020-03-16 0-15~ c 0 0 0 1 0 0 1 1 #> 5 2020-03-16 0-15~ d 0 0 0 0 0 0 0 0 #> 6 2020-03-16 0-15~ SA 3 0 0 0 0 0 3 3 #> 7 2020-03-16 0-15~ a 3 0 0 0 0 0 3 3 #> 8 2020-03-16 0-15~ b 0 0 0 0 0 0 0 0 #> 9 2020-03-16 0-15~ c 0 0 0 0 0 0 0 0 #> 10 2020-03-16 0-15~ d 0 0 0 0 0 0 0 0 #> 11 2020-03-16 0-15~ RE 0 1 1 0 0 0 2 2 #> 12 2020-03-16 0-15~ a 0 0 0 0 0 0 0 0 #> 13 2020-03-16 0-15~ b 0 0 0 0 0 0 0 0 #> 14 2020-03-16 0-15~ c 0 1 1 0 0 0 2 2 #> 15 2020-03-16 0-15~ d 0 0 0 0 0 0 0 0 #> 16 2020-03-16 <NA> XA 0 0 0 0 1 1 2 2 #> 17 2020-03-16 <NA> a 0 0 0 0 0 0 0 0 #> 18 2020-03-16 <NA> b 0 0 0 0 0 1 1 1 #> 19 2020-03-16 <NA> c 0 0 0 0 0 0 0 0 #> 20 2020-03-16 <NA> d 0 0 0 0 1 0 1 1 #> 21 2020-03-16 <NA> RE 0 0 0 0 0 2 2 2 #> 22 2020-03-16 <NA> a 0 0 0 0 0 0 0 0 #> 23 2020-03-16 <NA> b 0 0 0 0 0 0 0 0 #> 24 2020-03-16 <NA> c 0 0 0 0 0 0 0 0 #> 25 2020-03-16 <NA> d 0 0 0 0 0 2 2 2 #> # ... with 1 more variable: percent <chr> 用彩条创建数据的轮廓图。我已经成功完成了无数次,即使使用相同变量的其他层也是如此。但是,当值变小时(约为1E-12),轮廓的某些部分将显示白色。颜色栏也不显示白色。有谁知道这是什么原因以及如何解决?故障轮廓附在下面。

Bad contour

matplotlib.pyplot.contourf

2 个答案:

答案 0 :(得分:4)

tl; dr

鉴于新信息,matplotlib无法为您的数据设置正确的级别数(在文档中为see parameters),从而使数据不被绘制。要解决此问题,您需要告诉matplotlib使用plt.contourf(..., extend="max")plt.contourf(..., extend="both")

扩展限制

enter image description here

广泛的答案

contourf()显示带有colormap that doesn't include white的白色区域有几个原因。

NaN值

从不绘制NaN值。

enter image description here

屏蔽的数据

如果在绘图之前屏蔽数据,则数据不会出现在绘图中。但是您应该知道是否屏蔽了数据。

enter image description here

尽管如此,如果您使用Tick locator = LogLocator()之类的东西,则可能会掩盖您的数据。

enter image description here

Matplotlib无法为您的数据设置正确的级别

有时matplotlib设置的级别不正确,从而使您的某些数据无法绘制。

enter image description here

要解决此问题,您可以使用plt.contourf(..., extend=EXTENDS),其中EXTENDS可以是"neither", "both", "min", "max"

enter image description here

粗网格

contourf plots whitespace over finite data. Past answers do not correct

答案 1 :(得分:0)

如果X和Y向量数据点的间距不相等,则图中的白色部分也会出现。在那种情况下,最好使用函数tricontourf()。

相关问题