0
$\begingroup$

Suppose I have a plot something like this:

data1 = {{1, 2}, {2, 1}, {3, 1}, {4, 2}}; 
ListLinePlot[data1, PlotStyle -> {Black}, Frame -> True, 
 FrameStyle -> Thick, GridLines -> Automatic, 
   PlotMarkers -> {"\[FilledSquare]", Large}, 
 PlotLegends -> {"Yager"}]

I want to change numerical $ x $-axis to strings $ x $-axis. Any suggestion?

The purpose of the question is like the following picture: Plot

$\endgroup$
1
  • $\begingroup$ Possible duplicate: (5369) $\endgroup$
    – Mr.Wizard
    Dec 15, 2018 at 18:05

3 Answers 3

2
$\begingroup$

The tick labels are centered at the requested position. If you want the base of the subscript centered instead, you can add an invisible pre-subscript. Here is a function to do this:

centeredSubscript[b_, s_] := Row[{Invisible[Subscript["", s]], Subscript[b, s]}]

Then, adjusting some options and using the above function:

data1 = {
    {{1, 4}, {2, 1}, {3, 2}, {4, 3}},
    {{1, 4}, {2, 1}, {3, 3}, {4, 2}}
}; 
ListLinePlot[
    data1,
    Axes -> True, 
    AxesStyle -> Thick,
    GridLines -> {{1,2,3,4}, {1,2,3}},
    GridLinesStyle -> {Dashed, None},
    PlotMarkers -> {{"\[FilledSquare]", Large}},
    AxesOrigin -> {0, 0},
    Ticks -> {Table[{i, centeredSubscript["A", i], {0, .02}}, {i, 4}], Table[{i, i, {0, .02}}, {i, 0, 4}]},
    TicksStyle -> Directive[Bold, 16]
]

enter image description here

$\endgroup$
1
$\begingroup$
data1 = {{1, 2}, {2, 1}, {3, 1}, {4, 2}};
xs = First /@ data1
labels = ToString /@ Subscript[A, #] & /@ xs
xticks = Transpose[{xs, labels}]
ListLinePlot[data1, PlotStyle -> {Black},
 FrameTicks -> {xticks, Automatic},
 Frame -> True, FrameStyle -> Thick, GridLines -> Automatic, 
 PlotMarkers -> {"\[FilledSquare]", Large}, PlotLegends -> {"Yager"}]
$\endgroup$
2
  • $\begingroup$ Is there any way Subscript[A, 1] move forward on the x-axis like the picture? $\endgroup$
    – bahram
    Dec 15, 2018 at 19:16
  • $\begingroup$ @bahram Is this close enough? labels = (" " <> ToString[Subscript["A", #], StandardForm] &) /@ ToString /@ xs $\endgroup$
    – Alan
    Dec 15, 2018 at 20:56
0
$\begingroup$

Use FrameTicks:

data1 = {{1, 2}, {2, 1}, {3, 1}, {4, 2}};
 ListLinePlot[data1,
   PlotStyle -> {Black},
   Frame -> True,
   FrameStyle -> Thick,
   GridLines -> Automatic,
   PlotMarkers -> {"\[FilledSquare]", Large},
   PlotLegends -> {"Yager"},
   FrameTicks->{{Automatic,None},{{{1,"Subscript[A, 1]"},
   {2,"Subscript[A, 2]"},{3,"Subscript[A, 3]"},{4,"Subscript[A, 4]" }},None}}
 ]   

enter image description here

$\endgroup$
2
  • $\begingroup$ Is there any way Subscript[A, 1] move forward on the x-axis like the picture? $\endgroup$
    – bahram
    Dec 15, 2018 at 19:09
  • $\begingroup$ @bahramThe only way I can think of is to use spaces, i.e., " Subscript[A, 1]" and experiment with the number of spaces that gives you the right look. $\endgroup$
    – Themis
    Dec 15, 2018 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.