1

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

bahram
  • 185
  • 6

3 Answers3

2

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

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
1
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"}]
Alan
  • 13,686
  • 19
  • 38
  • Is there any way Subscript[A, 1] move forward on the x-axis like the picture? – bahram Dec 15 '18 at 19:16
  • @bahram Is this close enough? labels = (" " <> ToString[Subscript["A", #], StandardForm] &) /@ ToString /@ xs – Alan Dec 15 '18 at 20:56
1

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

Themis
  • 778
  • 4
  • 10
  • Is there any way Subscript[A, 1] move forward on the x-axis like the picture? – bahram Dec 15 '18 at 19:09
  • @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. – Themis Dec 15 '18 at 23:50