本文介绍了创建函数以灵活定义Mathematica中的图表选项列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一些功能,以避免我为不同类型的剧情重复剧情选项。当我尝试创建机制来自动处理Frameticks&绘制范围给定在给定绘图内考虑的数据。

pre $ 模块[{chartData},
chartData = RandomInteger [20 ,20];

BarChart [chartData,
Frame - > {{True,True},{True,True}},
ImageSize - > 300,
ChartStyle - >黑色,
FrameLabel - > {{yName,None},{None,xName}},
ChartElementFunction - > FadingRectangle,
LabelStyle - >指令[Black,Bold,18],
PlotRange - > {Automatic,1.3*Max@chartData},
FrameTicks - > {{{Round @ Min @ chartData,Round @(Max @ chartData / 2),
Round @ Max @ chartData},None},
{{0,Round @(Length @ chartData / 2) ,Length @ chartData},None}}
]]


现在,我试图简化我的生活:

  chartOptions [yName_,xName_]:= {Frame  - > {{True,True},{True,True}},
ImageSize - > 300,
ChartStyle - >蓝色,
FrameLabel - > {{yName,None},{None,xName}},
ChartElementFunction - > FadingRectangle,
LabelStyle - >指令[Black,Bold,18],
FrameTicks - > {{{Round @ Min @ chartData,Round @(Max @ chartData / 2),
Round @ Max @ chartData},None},
{{0,Round @(Length @ chartData / 2) ,
Length @ chartData},None}},
PlotRange - > {Automatic,1.3*Max@chartData}}

这样,希望我的实际图表代码是这样的:

 模块[{chartData},
chartData = RandomInteger [20,20];

BarChart [chartData,
chartOptions [yName,xName]]]

但这不起作用:



想法是,graphOptions函数将根据实际绘图的图表数据进行调整。



我现在试图只使用我真正理解的东西,所以我希望这样的问题不需要太多复杂的技巧:-)!
chartOptions 变量 chartData 是一个引用全局定义的自由变量。在 BarChart 表达式中, chartData 变量被本地化为包含模块,因此对 chartOptions 。将 chartOptions 更改为接受 chartData 作为参数:

  chartOptions [chartData_,yName_,xName]:= ... 

然后相应地调整 BarChart 表达式:

  Module [{chartData },chartData = RandomInteger [20,20]; 
BarChart [chartData,chartOptions [chartData,yName,xName]]]


I am trying to create some functions that will avoid me to repeat plot options for different types of plot. I encounter some troubles as I try to create mechanisms to automatically handle Frameticks & Plot Range given the data considered within a given plot.

Module[{chartData},
        chartData = RandomInteger[20, 20];

BarChart[chartData,
                Frame -> {{True, True}, {True, True}},
                ImageSize -> 300,
                ChartStyle -> Black,
                FrameLabel -> {{"yName", None}, {None, "xName"}},
                ChartElementFunction -> "FadingRectangle",
                LabelStyle -> Directive[Black, Bold, 18],
                PlotRange -> {Automatic, 1.3*Max@chartData},
                FrameTicks -> {{{Round@Min@chartData, Round@(Max@chartData/2),
                                Round@Max@chartData}, None},
                              {{0, Round@(Length@chartData/2), Length@chartData}, None}}
          ]]

Now Here is my attempt to simplify my life :

chartOptions[yName_, xName_] := {Frame -> {{True, True}, {True, True}},
                                ImageSize -> 300,
                                ChartStyle -> Blue,
                                FrameLabel -> {{yName, None}, {None, xName}},
                                ChartElementFunction -> "FadingRectangle",
                                LabelStyle -> Directive[Black, Bold, 18],
                                FrameTicks -> {{{Round@Min@chartData, Round@(Max@chartData/2),
                                                 Round@Max@chartData}, None},
                                              {{0,Round@(Length@chartData/2),
                                               Length@chartData}, None}},
                                PlotRange -> {Automatic, 1.3*Max@chartData}}

This so that hopefully my actual Chart code is as such :

 Module[{chartData},
         chartData = RandomInteger[20, 20];

         BarChart[chartData,
         chartOptions["yName", "xName"]]]

But this does not work :

The idea is that the chartOptions function will be adjusted given the chart data of the actual plot where it is used.

I am now trying to only use things I trully understand so I hope such a problem does not require to much of your sophisticated skills :-) !

解决方案

The problem is that in chartOptions the variable chartData is a free variable referring to a global definition. In the BarChart expression, the chartData variable is localized to the containing module and is thus invisible to chartOptions. Change chartOptions to accept chartData as a parameter:

chartOptions[chartData_, yName_, xName] := ...

and then adjust the BarChart expression accordingly:

Module[{chartData},chartData=RandomInteger[20,20];
  BarChart[chartData,chartOptions[chartData, "yName","xName"]]]

这篇关于创建函数以灵活定义Mathematica中的图表选项列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 20:03