List name to String in Mathematica - string

Considering :
daList=Range[10]
What I need is the title to be the list name but trying :
ListPlot[daList, PlotLabel -> ToString[daList]]
does not seem to work.
EDIT
"daList" is the Title I want. SORRY for previous my previous lack of precision
EDIT
I yet can`t make any solution work but I think I isolated the problem. The list name is a plotting Function argument. I believe the simple version replicating my problem is like :
list = {1, 2, 3, 4};
naming[list_] := ToString[HoldForm[list]];
naming[list]
an below my "real" code :
sequenceCountPlot[conditionSet_] :=
ListPlot[sequenceCountALL[conditionSet],
plotOptions[
("DisplayNo looking outside filter" <> (ToString[HoldForm[conditionSet]])),
"Number of Display",
"Filter Radius in Cm",
prefCOLORS],
PlotRange -> {{0, 10}, {0, Max#(Max /# sequenceCountALL[conditionSet])}}, Joined -> True]
Where plotOptions is a function to customize some options (Titles and Colors) and feet the plot with other.
Please know that Even with Evaluate[plotOptions] the results remain the same.

Try this:
ToString[HoldForm#daList]
so eg
ListPlot[daList, PlotLabel -> ToString[HoldForm#daList]]

I believe that two the most convenient ways to do this are:
daList = Range[10];
ListPlot[daList, PlotLabel -> "daList"]
ListPlot[daList, PlotLabel -> HoldForm[daList]]
Other possibilities:
ListPlot[daList, PlotLabel -> MakeBoxes[daList]]
ListPlot[daList, PlotLabel -> SymbolName[Unevaluated#daList]]
ListPlot[daList, PlotLabel -> ToString[Unevaluated#daList]]
ListPlot[daList, PlotLabel -> ToString[HoldForm#daList]]

Another possibility:
ListPlot[daList, PlotLabel -> ToString[Unevaluated[daList]]]

may be
ListPlot[daList, PlotLabel -> StringJoin[Map[ToString, daList]]]

Related

Individual point coloring in ListPlot, ErrorListPlot in Mathematica

I can get a colored ListLinePlot by doing something like
ListLinePlot[Range[420, 680, 20], ColorFunction -> "VisibleSpectrum", ColorFunctionScaling -> False]
However, as indicated by the help file ("ColorFunction requires at least one dataset to be Joined"), if I do the equivalent
ListPlot[Range[420, 680, 20], ColorFunction -> "VisibleSpectrum", ColorFunctionScaling -> False]
all my points are blue. Is there a nice way to get ColorFunction to work for ListPlot with Joined -> False?
That is, is there a nicer way to get something like
ListPlot[
List /# Transpose[{Range[(680 - 420)/20 + 1], Range[420, 680, 20]}],
PlotMarkers -> ({Graphics[{#, Disk[]}], 0.05} & /# ColorData["VisibleSpectrum"] /# Range[420, 680, 20])
]
?
(Also, does anyone have an explanation of why Mathematica requires Joined -> True in order to make use of ColorFunction?)
Edit: I'm also looking for a way to do a similar coloring with ErrorListPlot in the ErrorBarPlots package.
The problem is, that Joined->True draws a Line[] which can be given VertexColors for each containing point. I assume doing the same for the points when setting Joined->False leads to situations where it does not work. Nevertheless, Line[] and Point[] work pretty much the same in your case. So what is about
ListLinePlot[Range[420, 680, 20], ColorFunction -> "VisibleSpectrum",
ColorFunctionScaling -> False] /. Line[arg___] :> Point[arg]
And, by the way, if your using a ListLinePlot only, where the only Line[] directives arising are the one from your data, this should work even if you have more datasets and {x,y} coordinates
data = Transpose[Table[{{x, Sin[x]}, {x, Cos[x]}}, {x, 0, 2 Pi, 0.2}]];
ListLinePlot[data, ColorFunction -> Hue] /. Line[arg___] :> Point[arg]
You can use DiscretePlot:
data = Range[420, 680, 20];
DiscretePlot[data[[i]], {i, Length[data]},
ColorFunction -> "VisibleSpectrum", ColorFunctionScaling -> False,
Filling -> None]
If you're plotting a list of x,y points, it gets a little trickier:
data = Transpose[{Range[420, 680, 20], Range[400, 530, 10]}];
mapping = Apply[Rule, data, 2];
DiscretePlot[i/.mapping, {i, data[[;;,1]]},
ColorFunction -> "VisibleSpectrum", ColorFunctionScaling -> False,
Filling -> None]
It does seem rather odd that DiscretePlot will let you color the points differently whereas ListPlot won't. I'm sure it must have something to do with the implementation details, but I can't think of a reason why that would be the case.
I came across this problem in my work too. I assign a colour to each point in the following manner:
data = ...
ListPlot[data] /. Point[args___] :> Point[args, VertexColors -> {c1, c2, ...}]
where c1 is the colour for the first data point, and so on. The colour list may be programmatically generated, eg
ColorData["Rainbow"] /# (Range#Length#data / Length#data)
Here is the result.
The good points of this method are as follows.
It's straightforward: we have a list of pairs, then we create a corresponding list of colours.
Our original ListPlot code needs not be modified (eg, changed into ListLinePlot).

Mathematica Dashing "Up and down"

Is there any reason that underlies mathematica's way of presenting this graph
ListPlot[
Table[{x, x*01}, {x, -5, 5, .08}],
PlotStyle -> White,
Filling -> 0,
FillingStyle -> {Dashed, Brown}]
While the dashing is present for the part of the graph above the zero boundary, another part of the graph has the filling that is solid.
Am I doing something wrong?
Not that wrong. Mathematica is interpreting your filling style as being Dashed below zero and Brown above. You just need another pair of braces, like so:
ListPlot[Table[{x, x*01}, {x, -5, 5, .08}], PlotStyle -> White,
Filling -> 0, FillingStyle -> {{Dashed, Brown}}]
Hope that helps.

Why doesn't the Inset item show in Prolog in a plot?

I apologise for the length of this question. My current Mathematica programming project involves replicating the very specific business rules my employing institution has for publication-quality plots of time series. One of these rules is that multipanel graphs have panel titles and subtitles centered near the top of the plot area. This worked fine using Prolog and Inset until I redid that bit of the function to allow explicit Prolog elements to be included as well. Then only the item passed in the Prolog option shows, not the panel titles and subtitles. If there is no explicit Prolog option, the panel titles and subtitles show up fine.
To explicate a bit further, imagine the function body is all in a big Module, including the local variable pl=OptionValue[Prolog] and mp=OptionValue[MultiPanel].
A bit further down is this definition (ppl, title and subtitle were also defined as local variables, and Rfont and framecol are constants defined elsewhere in the package, namely to be "Arial" and "Black"):
ppl=If[mp===False,pl,If[Length[pl]>0,Join[{
If[ToString[subtitle] == "None", Inset[
DisplayForm[GridBox[{{Style[title, 20, FontFamily -> Rfont, framecol]}}]],
Scaled[{0.5,0.96}],{Center,Top} ],
Inset[DisplayForm[
GridBox[{{Style[title, 20, FontFamily -> Rfont, framecol]},
{Style[subtitle, 16, FontFamily -> Rfont, framecol]}},
RowSpacings -> 0]],Scaled[{0.5,0.98}],{Center,Top}] ]},pl],
{If[ToString[subtitle] == "None",
Inset[ DisplayForm[
GridBox[{{Style[title, 20, FontFamily -> Rfont, framecol]}}]],
Scaled[{0.5,0.96}],{Center,Top} ],
Inset[DisplayForm[
GridBox[{{Style[title, 20, FontFamily -> Rfont, framecol]},
{Style[subtitle, 16, FontFamily -> Rfont, framecol]}},
RowSpacings -> 0]],Scaled[{0.5,0.98}],{Center,Top}]] }] ]
All fine as far as it goes. The Inset is used if the option MultiPanel (and thus mp) is not False and there is no Prolog explicitly set. But if a prolog is set, e.g., Prolog -> {Text["test", Scaled[{0.6, 0.5}]]}, then the Inset doesn't show, only the Text element.
It isn't a problem with the way I am joining them together. Capturing and printing the value of ppl gives:
{Inset[{{Style["This is a test", LineColor -> GrayLevel[0],
FrontFaceColor -> GrayLevel[0], BackFaceColor -> GrayLevel[0],
GraphicsColor -> GrayLevel[0], FontFamily -> "Arial", FontSize -> 20,
FontColor -> GrayLevel[0]]},
{Style["Year-ended percentage change", LineColor -> GrayLevel[0],
FrontFaceColor -> GrayLevel[0], BackFaceColor -> GrayLevel[0],
GraphicsColor -> GrayLevel[0], FontFamily -> "Arial", FontSize -> 16,
FontColor -> GrayLevel[0]]}},
Scaled[{0.5, 0.98}], {Center, Top}], Text["test", Scaled[{0.6, 0.5}]]}
(And yes, those are a bunch of undocumented options which I shall ask about in another question.)
Does anyone know if there is anything preventing Prolog (or Epilog for that matter) combining Inset elements and other things in the one set of graphics options?
As you have not given a working fragment of code, I'm not sure what's going wrong in it.
How does my simple example not replicate what you're trying?
plot = Plot[x^2, {x, -5, 5}, Prolog -> Inset[Style[Text[":) = \[HappySmiley]"], Large]]]
pl = Flatten#{Prolog /. AbsoluteOptions[plot, Prolog]}
join = Show[{Plot[x^2 + 1, {x, -5, 5}, PlotStyle -> Red],
Plot[x^2, {x, -5, 5}, PlotStyle -> Blue]},
Prolog -> Join[{Inset[Graphics#Circle[]]}, If[Length[pl] > 0, pl, {}]]]
I feel a bit stupid now. I worked out the issue. Let me see if I can explain clearly enough.
Suppose you have a custom function (say, XYZPlot) that is built on a built-in one (say, DateListPlot). Suppose you want to pass some options to DateListPlot, as well as handling some options that are specific to XYZPlot. Then you probably want to pass any options the user includes explicitly before you pass the default options that apply to your custom function. This is a toy example: normally you'd set default PlotRange as an option to your custom function with SetOptions etc.
defaultplotrange = {1,5};
XYZPlot[args___,opts:OptionsPattern[{XYZPlot,DateListPlot}]]:=
DateListPlot[args,Sequence ## FilterRules[{opts}, Options[DateListPlot]],
PlotRange->defaultplotrange]
So far so good. But if you want to catch the value of an option and modify it even if the user provides an explicit value for it when they call the function, for example to add [ahem] panel titles if some other option is set to True, then you can't put that option after the FilterRules bit. It has to come before. Otherwise the value of Prolog that the user gave will be used, not the one with the bit added. So the right way is to do something like this.
defaultplotrange = {1,5};
XYZPlot[args___,opts:OptionsPattern[{XYZPlot,DateListPlot}]]:=
With[{pl=OptionValue[Prolog],mp=OptionValue[MultiPanel]},
DateListPlot[args,
Prolog -> If[mp===False,pl,Join[pl,{Inset[(* something else *)]}]],
Sequence ## FilterRules[{opts}, Options[DateListPlot]],
PlotRange->defaultplotrange]]
Sorry to bother you all.

Arrange & Display Parts of Grids in Mathematica

In order to find quickly the location of my data, I display a table with my variables names as well as informations about each.
Since I have a lot of columns (variable) I copy and paste blocks of it in a cell to have them all on 1 screen.
I would like to code this, such that I would input several range of Rows to be extracted and efficiently displayed some how on a Grid that would fit an area of the screen ? I have failed yet to display 2 grid nicely together.
In case I did not express my problem properly above, here is a simple example:
How can I move the blue part to the side of the pink one if the output of laList is what we have to deal with ?
co1 = Range[6];
co2 = Range[11, 16];
co3 = {"A", "B", "C", "D", "E", "F"};
laList = Join[{co1}, {co2}, {co3}] // Transpose;
laListGraph = Grid[laList,
Dividers -> All,
Alignment -> {Left, Center},
ItemSize -> Automatic,
ItemStyle -> Directive[FontSize -> 14, Black, Italic, Bold],
Spacings -> {2, 1},
Background -> {None, None, {
{{1, 3}, {1, 3}} -> LightRed,
{{4, 6}, {1, 3}} -> LightBlue
} } ]
EDIT:
On second thoughts, what I had earlier is not what you wanted... you want it displayed as columns, but with the second half of the rows split and displayed beside the first. The following code should do that. Let me know if this is what you had in mind...
(Grid[#1, Dividers -> All, Alignment -> {Left, Center},
ItemSize -> Automatic,
ItemStyle -> Directive[FontSize -> 14, Black, Italic, Bold],
Spacings -> {2, 1},
Background -> {None,
None, {{1, 3}, {1, 3}} -> #2}] &) ### {{laList[[;; 3, All]],
LightRed}, {laList[[4 ;;, All]], LightBlue}} // Row
As I understood your question, the coloring was there just to show us which portion you want to "move up". So:
showG[l_List] :=
Grid[Join[
l[[ ;; IntegerPart[Length#l/2]]],
l[[IntegerPart[Length#l/2] + 1 ;;]]
, 2], Frame -> All];
showG[laList]
Edit
Or more to #Mr.'s taste:
showG[l_List] :=
Grid[Join[l[[ ;; #]], l[[# + 1 ;;]], 2], Frame -> All] &# Floor[Length#l/2];
Here is how I would do it. Starting with your laList as defined in the question:
laList2 = ArrayFlatten # {Partition[laList, 3]};
Grid[laList2,
Dividers -> All,
Alignment -> {Left, Center},
ItemSize -> Automatic,
ItemStyle -> Directive[FontSize -> 14, Black, Italic, Bold],
Spacings -> {2, 1},
Background -> {None, None, {
{{1, 3}, {1, 3}} -> LightRed,
{{1, 3}, {4, 6}} -> LightBlue
} }
]
Please notice:
the value 3 within Partition will need to be adjusted according to your list.
the region specification for the LightBlue area was reversed.
A variation of yoda's code I like is:
subgrid= Grid[#1,
Dividers -> All,
Alignment -> {Left, Center},
ItemSize -> Automatic,
ItemStyle -> Directive[FontSize -> 14, Black, Italic, Bold],
Spacings -> {2, 1},
Background-> #2] &;
MapThread[subgrid, {Partition[laList, 3], {LightRed, LightBlue}}] //Row
Also, with this method you can partition a list that does not divide evenly:
MapThread[subgrid, {
Partition[laList, 4, 4, 1, {}],
{LightRed, LightBlue}
}] //Row

Graph[] cuts off vertex labels in Mathematica

Graph[] has a tendency to cut off vertex labels in Mathematica. I am looking for a robust workaround.
Example:
Graph[{1 -> 2, 2 -> 3, 3 -> 1}, VertexLabels -> "Name"]
My present workaround:
SetOptions[Graph, ImagePadding -> 12]
This is not robust because the value of ImagePadding needs to be manually adjusted depending on the label size.
Apparently using FullGraphics on the Graph object will fix the clipping for the purpose of display, at the expense of interactivity.
Per the comment below, Show[] works as well, and avoids modifying the graphics.
Here are two possible workarounds.
Enlarge the vertex size and place the labels within the vertex. Of course, this also depends on the length of the labels, but for shortish labels it works well, whereas your example above clips off any label of more than one character for vertex 1.
ex:
Table[Graph[{1 -> 2, 2 -> 3, 3 -> 1}, VertexSize -> 0.3,
VertexLabels -> Table[i ->
Placed["vertex" <> ToString[i], p], {i, 3}],
VertexShapeFunction -> "Square", PlotLabel -> p],
{p, {Left, Top, Right, Bottom, Center}}]
Use tooltips to store the labels instead of displaying them on the graphic. [Edit: Center probably looks the best, and then you can wrap labels by putting \n in your string if you need to, but again, depends on the label length.]
ex:
Graph[{1 -> 2, 2 -> 3, 3 -> 1}, VertexLabels -> Placed["Name", Tooltip]]
While this stops you from being able to see all the labels at the same time, you never have any clipping.

Resources