【问题标题】:Measure the height of bar chart using python or R使用python或R测量条形图的高度
【发布时间】:2021-11-24 08:08:27
【问题描述】:

我需要的一组研究数据不可用,所以我必须从下面的条形图中获得对数据的估计。不知道有没有办法使用Python或者R来测量每个条的高度,这样我就可以根据Y轴计算出对应的值了?

由于担心错误和整个图的大尺寸(大部分部分未在此处显示),我无法手动执行此操作。

非常感谢!

【问题讨论】:

  • 您好!也许magick 包可以提供帮助。
  • 它可以工作。问题是,这张图表的质量是否比您在问题中提出的要好?
  • @MarekFiołka,是的,我可以在我的电脑上放大图像并将其裁剪成碎片,然后它应该有更好的质量。你想让我在这里更新这个数字吗?
  • 请给它最好的质量。
  • 我使用在线工具可能更简单。使用带有以下术语的搜索引擎:数字化图形图像。

标签: python r


【解决方案1】:
library(tidyverse)
library(png)
png = readPNG("GsWv1.png", native = FALSE, info = FALSE)

a1 = (2882-191)/27
b1 = 191-a1
a2 = (2916-230)/27
b2 = 230-a2

ncol1 = (1:28)*a1+b1
ncol2 = (1:28)*a2+b2
nrows = 9:413
rows=9:413
scale=98/300

fbar = function(png, rows, col){
  col = round(round(png[rows,col,1], 2)+ 
                round(png[rows,col,2], 2)+
                round(png[rows,col,3], 2), 2)
  sum(col>0.5 & col <1.5)
}


df = tibble(data = 1:28) %>% 
  mutate(redval = map(data, ~fbar(png, rows, .x*a1+b1)),
         greenval = map(data, ~fbar(png, rows, .x*a2+b2))) %>% 
  unnest(c(redval, greenval)) %>% 
  mutate(data = data %>% factor(), #Adjust this mutation
         redval=redval*scale, 
         greenval = greenval*scale) %>% 
  pivot_longer(redval:greenval) %>% 
  mutate(name = name %>% fct_inorder())

df %>% ggplot(aes(data, value, fill=name))+
  geom_bar(stat="identity",  position=position_dodge())

注意!这仅适用于 png 大小的 998x132 点!您可能需要稍微调整这些值。在那里做,我写了评论。

更新 1

哎呀!我在我的cmets中犯了一个小错误。 png 文件大小必须恰好为 2,956 x 418 像素。以防万一,我附上从下面这张图表中读取的数据。

structure(list(data = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 
12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L, 
18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 23L, 24L, 24L, 
25L, 25L, 26L, 26L, 27L, 27L, 28L, 28L), .Label = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", 
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", 
"26", "27", "28"), class = "factor"), name = structure(c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("redval", "greenval"), class = "factor"), 
    value = c(19.6, 17.64, 32.0133333333333, 33.32, 18.2933333333333, 
    16.66, 43.12, 37.8933333333333, 47.04, 35.9333333333333, 
    48.3466666666667, 42.7933333333333, 20.9066666666667, 15.3533333333333, 
    70.8866666666667, 29.0733333333333, 11.4333333333333, 6.86, 
    114.66, 114.986666666667, 48.3466666666667, 35.9333333333333, 
    90.16, 84.28, 18.62, 20.9066666666667, 23.52, 30.7066666666667, 
    42.7933333333333, 45.4066666666667, 41.8133333333333, 13.0666666666667, 
    46.06, 50.96, 38.5466666666667, 25.8066666666667, 46.7133333333333, 
    20.9066666666667, 32.0133333333333, 26.7866666666667, 83.9533333333333, 
    83.6266666666667, 61.74, 63.0466666666667, 10.4533333333333, 
    6.53333333333333, 85.26, 24.5, 40.8333333333333, 37.24, 2.94, 
    2.94, 19.6, 16.66, 25.1533333333333, 19.6)), row.names = c(NA, 
-56L), class = c("tbl_df", "tbl", "data.frame"))

下面我还附上我从中读取值的原始图片。

【讨论】:

  • 对不起,我仍然无法正常工作。我将 png 的大小调整为 998x132。但是当我将它读入 R 时,它的维度是132 998 4。当我运行df = tibble(data = 1:28) ... 时,出现错误:`` 错误:mutate()redval 有问题。我redval = map(data, ~fbar(png, rows, .x * a1 + b1))。 x 下标越界。. I also tried png = png[,,1]``,但错误仍然存​​在。非常感谢您的帮助,谢谢!
  • 我已经更新了答案,现在一切正常。
  • 我很高兴能帮上忙。请记住,在 Stack Overflow 上,您可以通过选择已批准的解决方案来表示感谢。
  • 我真的很想批准它,我也看到其他人赞成它。但是在使用 png 的原始大小 2956x418 后,我在阅读时收到“文件不是 PNG 格式”的错误。我不想一直打扰你。但我还没有做到。
  • 将任何图形程序jpg转换为png。就这样。它必须工作!如果您在 Chrome 中工作,请右键单击图像并选择“将图像另存为...”。谷歌浏览器默认将文件保存为 png。
猜你喜欢
  • 2018-05-15
  • 1970-01-01
  • 1970-01-01
  • 2014-06-15
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 2013-03-22
相关资源
最近更新 更多