pitosalas
pitosalas

Reputation: 10882

How do I create a nice one dimensional graph?

df = pd.DataFrame({"labels": ["This is a long label", 
                     "And this is also a long label", 
                     "Wow its a label thats pretty long", 
                     "Can you follow the pattern here?"],
                   "values": [2.2, 1.2, 3.0, 4.1]})

In the real example I have about 15 labels and values. Given this I would like to display a single axis, marks at 2.2, 1.2, 3.0 and 4.1, each labeled with the corresponding label. My labels are long so I think they need to be displayed diagonally or with some other kind of device so the labels remain legible.

Clarification

To be clear I want a single axis and no graph field. It will be like a number line, with ticks on it for the values -- 2.2, 1.2, etc. And the labels annotating what the tics stand for.

Here's a very rough sketch of the idea: enter image description here

I am experimenting but have not found a way to do this pandas or seaborn. Is it even possible?

Upvotes: 3

Views: 1381

Answers (1)

Haleemur Ali
Haleemur Ali

Reputation: 28283

plot a scatter plot, hide the spines & x-axis use the annotate function to place the text. Finally, play with the positioning until the alignment seems right

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.xaxis.set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.scatter([0]*len(df), df['values'], marker='d', s=100, c='r')
ax.set_xlim(-0.1,5)
for _, r in df.iterrows():
    ax.annotate(r['labels'], xy=(0.20, r['values']))

vertical number line

Upvotes: 4

Related Questions