2d函数域的pyplot颜色图表

时间:2016-02-02 06:14:03

标签: matplotlib

如何跨两个维度制作函数域的二维颜色图?像这样:

def f(x):
  return x[0]**2+6*x[1]**2+x[0]*x[1]+np.sin(x[0])+3*x[0]
x = np.arange(-5,5,0.1)
y = np.arange(-5,5,0.1)
plt.contours(x,y,f([x,y])

1 个答案:

答案 0 :(得分:2)

将您的最后一行更改为

library(shinydashboard) library(shiny) body <- dashboardBody( fluidRow( valueBox( uiOutput("bens"), "Benef reg", icon = icon("users"), color = "green") )) server <- function(input, output) { ## beneficiaries output$bens <- renderText({ prettyNum(150, big.mark=",") })} shinyApp( ui = dashboardPage( dashboardHeader(), dashboardSidebar(), body), server = server) }

这将在x和y的网格网格上评估plt.contour(f(np.meshgrid(x,y)))并绘制该函数的轮廓。关于在matplotlib is here中生成等高线图的教程。一般来说,那里的教程非常好,你经常可以找到你想要的东西。

如果您希望轴标有fx范围内的范围,则需要

y

如果您更喜欢“热图”样式到等高线图,则可以改为plt.contour(x,y,f(np.meshgrid(x,y)))

为了好玩,我扩展了范围并放大了函数中的sin分量并生成了等高线图和热图(参见输出)

plt.pcolormesh(f(np.meshgrid(x,y)))

轮廓输出

Contour plot

import matplotlib.pyplot as plt import numpy as np def f(x): return x[0]**2+6*x[1]**2+x[0]*x[1]+150*np.sin(x[0])+3*x[0] x = np.arange(-50,50,0.1) y = np.arange(-50,50,0.1) plt.contour(x,y,f(np.meshgrid(x,y))) plt.show() 输出

enter image description here