0

I have a csv that looks like this:

2   1111
4   926
8   914
16  933
32  911
64  912
128 1010
256 1010
512 1013
1024    1070
1025    921
1026    921
1027    920
1028    918
1029    917
1030    916
1031    922
1032    927
1033    929
1034    924
2048    1048

The first column is the X and the second column is the Y.

When I try to plot it, matplotlib will treat it as numbers thus giving interval.

I prefer to treat the first column as categorical (2, 4, 8, ..., 2048) with the same distance between each x values.

I tried converting X into categorical, but matplotlib still treat it as number:

x = pd.Series(line_data["element"]).astype("category")
plt.scatter(x, line_data["time"])

I also tried converting X into string, but it got sorted thus making the graph wrong.

What is the best approach for this problem?

Many Thanks!

1 Answer 1

2

Matplotlib can as of the current version not handle the categorical pandas datatype.

Options you have:

  • use strings
    (as pointed out in the question) This solution will work in matplotlib 2.2 or higher.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    df = pd.DataFrame({"x" : np.logspace(0,11,12, base=2).astype(int),
                       "y" : np.random.randint(900,1200,12)})
    plt.plot(df.x.astype(str),df.y)
    plt.show()
    
  • plot the data index
    and set the ticklabels according to the values.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    df = pd.DataFrame({"x" : np.logspace(0,11,12, base=2).astype(int),
                       "y" : np.random.randint(900,1200,12)})
    plt.plot(df.index,df.y)
    plt.xticks(df.index, df.x)
    plt.show()
    

In both cases the plot will look like

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

The latter one works perfectly. Matplotlib still sorts my X if I convert it into str. I am wondering why it does not happen in yours
As pointed out in the answer "This solution will work in matplotlib 2.2 or higher." What exactly is unclear about that?
My bad for not reading it properly and thanks for pointing it out!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.