簡體   English   中英

如何在數據着色器中設置正確的彩色圖像顏色?

[英]How can I set up the correct color image color in datashader?

我試圖 plot 一個給定的例子,來自關於Timeseries的數據着色器頁面。 我使用了包括本在內的所有代碼片段,並嘗試通過將img傳遞給plt.imshow(img)來 plot 與 matplotlib 的img

import datetime
import pandas as pd
import numpy as np
import xarray as xr
import datashader as ds
import datashader.transfer_functions as tf
from collections import OrderedDict

import matplotlib.pyplot as plt
...

cvs = ds.Canvas(x_range=x_range, y_range=y_range, plot_height=300, plot_width=900)
aggs= OrderedDict((c, cvs.line(df, 'ITime', c)) for c in cols)
img = tf.shade(aggs['a'])

plt.imshow(img)
plt.show()
plt.close()

我想,它會渲染一個圖像,如圖所示,具有白色背景和藍色圖形。 然而,結果看起來像這樣:

在此處輸入圖像描述 在此處輸入圖像描述

此外,我還嘗試了以下幾行,但我無法創建數據着色器頁面上顯示的示例圖像:


colors = ["red", "grey", "black", "purple", "pink",
          "yellow", "brown", "green", "orange", "blue"]
imgs = [tf.shade(aggs[i], cmap=[c]) for i, c in zip(cols, colors)]
tf.stack(*imgs)

如何正確設置圖像的顏色以便 plot、保存或使用它?

它在數據着色器示例中是如何工作的?

如何正確設置圖像的顏色以便 plot、保存或使用它?

簡單的答案是,如果你想 plot tf.shade在 Matplotlib 中使用 imshow 的imshow imshow ,你可以使用img.to_pil()將其轉換為 PIL 圖像:

imshow img.to_pil

If you don't convert it to PIL like that, the output of tf.shade() is an object of type datashader.transfer_functions.Image , a type of Xarray DataArray that stacks the R, G, B, and A channels of an圖像作為多維數組。 imshow可以顯示 RGBA 圖像,但不能以 Datashader 返回的堆疊 DataArray 格式顯示,因此它似乎只采用其中一個通道(R,也許?)並使用默認的 Matplotlib 顏色圖繪制,因此不同的 colors。 所以永遠不要將 tf.shade 的tf.shade直接傳遞給imshow 這永遠不會有用,而且有點不幸的是它會繪制任何東西,因為它具有誤導性。

可以安全地將底層二維聚合數組( cvs.line 的cvs.line )傳遞給imshow for Matplotlib 以進行顏色圖和顯示,但是(a)如果您不喜歡 Matplotlib 的默認值,您需要選擇自己的顏色圖Viridis 和 (b) output 將垂直翻轉,因為 Datashader 渲染到從左下角開始的坐標和 Matplotlib 從左上角開始的坐標:

顯示輸出

但是,如果您想要 Matplotlib 圖而不是這些選項中的任何一個,我建議使用Datashader 的本機 Matplotlib dsshow 繪圖支持將結果顯示為 Matplotlib 圖; 不需要像這樣處理任何中間步驟,作為獎勵,結果將是交互式的。

它在數據着色器示例中是如何工作的?

Datashader 文檔都是作為 Jupyter 筆記本編寫的,在這些示例中,使用 Jupyter/IPython 豐富的顯示支持來處理圖像顯示。 具體來說,數據着色器Image object 實現了_repr_html_() ,Jupyter/IPython 調用該方法在筆記本中顯示 object。 如果您希望按照PIL文檔中的說明輕松轉換為 PNG,您可以自己調用img.to_pil()

暫無
暫無

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

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