# Graph with strings as x axis

**URL:** <https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274>\
**Category:** Newbie\
**Created:** [March 5, 2020, 11:42am UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274 "2020-03-05T11:42:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 5, 2020, 11:42am UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/1 "2020-03-05T11:42:14Z")

</div>

To start, I know it’s not possible to have alphanumerical axis for graphs. I’ve based my code off the previous posts asking this but they aren’t that clear.  
Here’s the abstract problem:  
I have a vector of strings and a vector of doubles. How can I make a graph or something that looks like a graph? Should I just make a histogram and pretend it’s a graph? Should I overlay a histogram with the strings as bins onto a graph while deleting the graph’s x axis? Bonus points: red markers for the values, with not line connecting them.

\_ROOT Version:\_6.18/04  
_Platform:_ Windows  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 5, 2020, 11:56am UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/2 "2020-03-05T11:56:30Z")

</div>

Alternative question:  
I can draw a graph:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/5/7/57324ab672732a0f0e6901e6cdddb952726d137c.png)  
and a histogram:  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/a/5/a5282ef717aa29d7efbb4438d50ac2b4c7b699ee.png)  
But not both in one canvas/one run of the macro. If I put any option in the histogram’s Draw(), the graph stays, and if I pass no options to the histogram’s Draw(), the histogram is drawn as in the second image. I’ve tried Draw(“SAME”)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 5, 2020, 12:25pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/4 "2020-03-05T12:25:04Z")

</div>

[GraphDoubleString.C](https://root-forum.cern.ch/uploads/short-url/sHTVCxwFqsvj4K0OTbzeSttrz2l.C) (944 Bytes)

```auto
root [0] double x[3] = {1,2,3}; double y[3] = {1,5,2}; string colour[3] = { "Blue", "Red", "Green"}; auto gr = new TGraph(3,x,y);
root [1] gr->SetMarkerStyle(20)
root [2] .x GraphDoubleString.C(gr,colour)

```

 ![Screenshot 2020-03-05 at 13.23.25](https://root-forum.cern.ch/uploads/default/original/3X/d/e/debdf345fb7c647d84bf8e9292341e353d25ddcb.png)

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 5, 2020, 12:46pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/5 "2020-03-05T12:46:24Z")

</div>

I’ll try this, thank you. Is string\* col just a blank pointer and so when you call col[i] its always blank or something like that?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 5, 2020, 12:49pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/6 "2020-03-05T12:49:41Z")

</div>

It allows to pass the array of strings to the TExec function.

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 5, 2020, 1:17pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/7 "2020-03-05T13:17:46Z")

</div>

I’ve changed my arrays to double instead of Double\_t. Because I’m bringing in stuff from vectors, I’m filling the arrays with a for loop. Still a blank canvas.

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 5, 2020, 1:20pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/8 "2020-03-05T13:20:51Z")

</div>

Is there a problem here? Assume the vectors are valid when passed in.

```auto
void plot(vector<double> conc, vector<string> date) {
    //x and y axis of graph
    double x[conc.size()];
    double y[conc.size()];
    string z[date.size()];
    for (int i = 0; i < conc.size();i++) {
        x[i] = i;
        y[i] = conc.at(i);
        z[i] = date.at(i);
    }
    auto graph = new TGraph(conc.size(), x, y);
    graph->SetMarkerStyle(20);
    GraphDoubleString(graph,z);
    delete graph;
}

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 5, 2020, 1:32pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/9 "2020-03-05T13:32:17Z")

</div>

when you use the macro I sent you as I suggested in my first post, does it work for you ?

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 5, 2020, 1:37pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/10 "2020-03-05T13:37:14Z")

</div>

Yes, when I run that code it does work.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 5, 2020, 1:44pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/11 "2020-03-05T13:44:19Z")

</div>

So it should be used that way . Can you post something reproducing what you are doing ?

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 5, 2020, 1:54pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/12 "2020-03-05T13:54:04Z")

</div>

What does that mean? Should I post files, or code or a description?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 5, 2020, 2:32pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/13 "2020-03-05T14:32:00Z")

</div>

Some code I can run to test what you are doing. The macro I sent you works when used I instructed. You are surely using it in some other way. I need to see (and try) what you do.

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 5, 2020, 2:41pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/14 "2020-03-05T14:41:33Z")

</div>

[Link](https://drive.google.com/drive/folders/1PdHQbMkiZI3_hJ5n5rB2d3Oi__zudSSy?usp=sharing)  
Note the col global variable at the top of the macro, and the path you need to change in the macro’s list\_files function. The main function is at the bottom and the relevant functions to this bug are near the bottom. I’ve thrown your macro in but it’s not working. I’m going to check the arrays real quick.

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 5, 2020, 2:58pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/15 "2020-03-05T14:58:11Z")

</div>

I added a local col to DrawLabels(), should probably be removed

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 5, 2020, 3:12pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/16 "2020-03-05T15:12:07Z")

</div>

I made a simpler version  
[plot.C](https://root-forum.cern.ch/uploads/short-url/yLvgyC8jg86tE0HWdTlLP2LseUD.C) (1004 Bytes)

```auto
root [0] .x plot.C

```

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 5, 2020, 3:47pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/17 "2020-03-05T15:47:39Z")

</div>

Almost perfect.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/7/0/70dfb883fa10523ecfc9200427cfb9d403b73f2d.png)  
This might be inevitable, but the words are horizontal Can I make them vertical? Also, how can I save the canvas to pdf?

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 5, 2020, 3:51pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/18 "2020-03-05T15:51:44Z")

</div>

Got the text angle with t-\>SetTextAngle(90);

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 5, 2020, 4:03pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/19 "2020-03-05T16:03:57Z")

</div>

Is there a list of SetTextAlign and SetTextFont etc. options? If it’s in the documentation I must be missing it.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 5, 2020, 4:14pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/20 "2020-03-05T16:14:41Z")

</div>

Text attributes are described here:  
[https://root.cern/doc/master/classTAttText.html](https://root.cern/doc/master/classTAttText.html)

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 5, 2020, 4:19pm UTC](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274/21 "2020-03-05T16:19:36Z")

</div>

Ok I just used a for loop to test:  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/6/6/667fa1f9b2c503a31dd7707b7479f7f8f74505ff.png)  
How can I access the canvas to make it bigger and save it as a pdf progamatically?

[Next page](https://root-forum.cern.ch/t/graph-with-strings-as-x-axis/38274.md?page=2)
