如何在Plotnine中绘制2维密度 /热图?

发布于 2025-01-17 23:21:00 字数 670 浏览 4 评论 0原文

我正在尝试在plotnine 中重新创建以下图表。它要求我提供更多细节,但我不想分散对示例的注意力。我认为我想做的事情很明显。一位同事赋予了我一项职能。我对重写该函数不感兴趣。我想使用 sm 并使用plotnine而不是matplotlib来绘制它。我用plotnine绘制了很多数据帧,但我不确定在这种情况下如何使用它。我曾尝试自己解决这个问题,但我总是迷失方向。我希望对于更有经验的人来说,我忽略了一些简单的事情。

import matplotlib.pyplot as plt

def getSuccess(y,x):
    return((y*(-x))*.5+.5)

steps = 100
stepSize = 1/steps

sm = []

for y in range(steps*2+1):
    sm.append([getSuccess((y-steps)*stepSize,(x-steps)*stepSize) for x in range(steps*2+1)])

plt.imshow(sm)

plt.ylim(-1, 1)

plt.colorbar()
plt.yticks([0,steps,steps*2],[str(y) for y in [-1.0,0.0,1.0]])
plt.xticks([0,steps,steps*2],[str(x) for x in [-1.0,0.0,1.0]])
plt.show()

I am trying to recreate the following graph in plotnine. It's asking me for more details but I don't want to distract from the example. I think it's pretty obvious what I'm trying to do. I have been given a function by a colleague. I'm not interested in rewriting the function. I want to take sm and use plotnine to plot it instead of matplotlib. I plot lots of dataframes with plotnine but I'm not sure how to use it in this case. I have tried on my own to figure it out and I keep getting lost. I hope that for someone more experienced I am overlooking something simple.

import matplotlib.pyplot as plt

def getSuccess(y,x):
    return((y*(-x))*.5+.5)

steps = 100
stepSize = 1/steps

sm = []

for y in range(steps*2+1):
    sm.append([getSuccess((y-steps)*stepSize,(x-steps)*stepSize) for x in range(steps*2+1)])

plt.imshow(sm)

plt.ylim(-1, 1)

plt.colorbar()
plt.yticks([0,steps,steps*2],[str(y) for y in [-1.0,0.0,1.0]])
plt.xticks([0,steps,steps*2],[str(x) for x in [-1.0,0.0,1.0]])
plt.show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你不是我要的菜∠ 2025-01-24 23:21:00

您可以尝试geom_raster

我已获取您的合成数据 sm 并将其转换为 dataframe,因为 plotnine 需要它。

import pandas as pd
import numpy as np
from plotnine import *

df = pd.DataFrame(sm).melt()
df.rename(columns={'variable':'x','value':'density'}, inplace=True)
df.insert(1,'y',df.index % 201)

p = (ggplot(df, aes('x','y'))
  + geom_raster(aes(fill='density'), interpolate=True)
  + labs(x=None,y=None)
  + scale_x_continuous(expand=(0,0), breaks=[0,100,200], labels=[-1,0,1])
  + scale_y_continuous(expand=(0,0), breaks=[0,100,200], labels=[-1,0,1])
  + theme_matplotlib()
  + theme(
      text = element_text(family="Calibri", size=9),
      legend_title = element_blank(),
      axis_ticks = element_blank(),
      legend_key_height = 29.6,
      legend_key_width = 6,
  )
)
p.save(filename='C:\\Users\\BRB\\geom_raster.png', height=10, width=10, units = 'cm', dpi=400)

结果是:

在此处输入图像描述

You could try geom_raster.

I have taken your synthetic data sm and converted to a dataframe as plotnine will need this.

import pandas as pd
import numpy as np
from plotnine import *

df = pd.DataFrame(sm).melt()
df.rename(columns={'variable':'x','value':'density'}, inplace=True)
df.insert(1,'y',df.index % 201)

p = (ggplot(df, aes('x','y'))
  + geom_raster(aes(fill='density'), interpolate=True)
  + labs(x=None,y=None)
  + scale_x_continuous(expand=(0,0), breaks=[0,100,200], labels=[-1,0,1])
  + scale_y_continuous(expand=(0,0), breaks=[0,100,200], labels=[-1,0,1])
  + theme_matplotlib()
  + theme(
      text = element_text(family="Calibri", size=9),
      legend_title = element_blank(),
      axis_ticks = element_blank(),
      legend_key_height = 29.6,
      legend_key_width = 6,
  )
)
p.save(filename='C:\\Users\\BRB\\geom_raster.png', height=10, width=10, units = 'cm', dpi=400)

This result is:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文