我无法使用 python 数据着色器显示图形
I am not able to display graph using python datashader
我使用以下步骤下载并安装了 datasheder:
git clone https://github.com/bokeh/datashader.git
cd datashader
conda install -c bokeh --file requirements.txt
python setup.py install
在那之后,我有运行使用终端的代码,如`python data.py,但没有显示图表;没有显示任何内容。
我不确定这里的步骤是否正确,有人可以帮我显示图表吗?这是我的代码:
import pandas as pd
import numpy as np
import xarray as xr
import datashader as ds
import datashader.glyphs
import datashader.transfer_functions as tf
from collections import OrderedDict
np.random.seed(1)
num=10000
dists = {cat: pd.DataFrame(dict(x=np.random.normal(x,s,num),
y=np.random.normal(y,s,num),
val=val,cat=cat))
for x,y,s,val,cat in
[(2,2,0.01,10,"d1"), (2,-2,0.1,20,"d2"), (-2,-2,0.5,30,"d3"), (-2,2,1.0,40,"d4"), (0,0,3,50,"d5")]}
df = pd.concat(dists,ignore_index=True)
df["cat"]=df["cat"].astype("category")
df.tail()
tf.shade(ds.Canvas().points(df,'x','y'))
glyph = ds.glyphs.Point('x', 'y')
canvas = ds.Canvas(plot_width=200, plot_height=200, x_range=(-8,8)y_range=(-8,8))
from datashader import reductions
reduction = reductions.count()
from datashader.core import bypixel
agg = bypixel(df, canvas, glyph, reduction)
agg
canvas.points(df, 'x', 'y', agg=reductions.count())
tf.shade(canvas.points(df,'x','y',agg=reductions.count()))
tf.shade(canvas.points(df,'x','y',agg=reductions.any()))
tf.shade(canvas.points(df,'x','y',agg=reductions.mean('y')))
tf.shade(50-canvas.points(df,'x','y',agg=reductions.mean('val')))
agg = canvas.points(df, 'x', 'y')
tf.shade(agg.where(agg>=np.percentile(agg,99)))
tf.shade(np.sin(agg))
aggc = canvas.points(df, 'x', 'y', ds.count_cat('cat'))
aggc
tf.shade(aggc.sel(cat='d3'))
agg_d3_d5=aggc.sel(cat=['d3', 'd5']).sum(dim='cat')
tf.shade(agg_d3_d5)
我没有试过你的代码,但里面没有任何东西可以实际显示图像。每个 shade() 调用都会在内存中创建一个图像,但是这里什么也没有做。如果您在 Jupyter notebook 环境中并且 shade() 调用是单元格中的最后一项,它会自动显示,但常规 Python 提示没有这样的 "rich display" 支持。因此,您可以将它保存到磁盘上的图像文件中(使用例如 utils/export_image),或者您可以将 shade() 的结果分配给一个变量,然后将其传递给 Bokeh 或 Matplotlib 或其他绘图,就像您更喜欢。但是如果你想看到它,你必须对图像做一些事情。
我能够以这种方式在您的代码中生成 tf.shade 的情节之一。
from datashader.utils import export_image
img = tf.shade(canvas.points(df,'x','y',agg=reductions.count()))
export_image(img=img, filename='test1', fmt=".png", export_path=".")
这是test1.png
中的情节
我使用以下步骤下载并安装了 datasheder:
git clone https://github.com/bokeh/datashader.git
cd datashader
conda install -c bokeh --file requirements.txt
python setup.py install
在那之后,我有运行使用终端的代码,如`python data.py,但没有显示图表;没有显示任何内容。
我不确定这里的步骤是否正确,有人可以帮我显示图表吗?这是我的代码:
import pandas as pd
import numpy as np
import xarray as xr
import datashader as ds
import datashader.glyphs
import datashader.transfer_functions as tf
from collections import OrderedDict
np.random.seed(1)
num=10000
dists = {cat: pd.DataFrame(dict(x=np.random.normal(x,s,num),
y=np.random.normal(y,s,num),
val=val,cat=cat))
for x,y,s,val,cat in
[(2,2,0.01,10,"d1"), (2,-2,0.1,20,"d2"), (-2,-2,0.5,30,"d3"), (-2,2,1.0,40,"d4"), (0,0,3,50,"d5")]}
df = pd.concat(dists,ignore_index=True)
df["cat"]=df["cat"].astype("category")
df.tail()
tf.shade(ds.Canvas().points(df,'x','y'))
glyph = ds.glyphs.Point('x', 'y')
canvas = ds.Canvas(plot_width=200, plot_height=200, x_range=(-8,8)y_range=(-8,8))
from datashader import reductions
reduction = reductions.count()
from datashader.core import bypixel
agg = bypixel(df, canvas, glyph, reduction)
agg
canvas.points(df, 'x', 'y', agg=reductions.count())
tf.shade(canvas.points(df,'x','y',agg=reductions.count()))
tf.shade(canvas.points(df,'x','y',agg=reductions.any()))
tf.shade(canvas.points(df,'x','y',agg=reductions.mean('y')))
tf.shade(50-canvas.points(df,'x','y',agg=reductions.mean('val')))
agg = canvas.points(df, 'x', 'y')
tf.shade(agg.where(agg>=np.percentile(agg,99)))
tf.shade(np.sin(agg))
aggc = canvas.points(df, 'x', 'y', ds.count_cat('cat'))
aggc
tf.shade(aggc.sel(cat='d3'))
agg_d3_d5=aggc.sel(cat=['d3', 'd5']).sum(dim='cat')
tf.shade(agg_d3_d5)
我没有试过你的代码,但里面没有任何东西可以实际显示图像。每个 shade() 调用都会在内存中创建一个图像,但是这里什么也没有做。如果您在 Jupyter notebook 环境中并且 shade() 调用是单元格中的最后一项,它会自动显示,但常规 Python 提示没有这样的 "rich display" 支持。因此,您可以将它保存到磁盘上的图像文件中(使用例如 utils/export_image),或者您可以将 shade() 的结果分配给一个变量,然后将其传递给 Bokeh 或 Matplotlib 或其他绘图,就像您更喜欢。但是如果你想看到它,你必须对图像做一些事情。
我能够以这种方式在您的代码中生成 tf.shade 的情节之一。
from datashader.utils import export_image
img = tf.shade(canvas.points(df,'x','y',agg=reductions.count()))
export_image(img=img, filename='test1', fmt=".png", export_path=".")
这是test1.png