0

I wish to clarify two queries in this post.

I have a pandas df like below picture. enter image description here

1. Plotting problem : . When i try to plot column 0 with column 1, the values gets sorted.

example : in col_0 I have values starting from 112 till 0. the values gets sorted in ascending order and the graph shows reversed X axis plot when i use the below code.

plt.plot(df.col_0, df.col_1)

enter image description here

What will be best way to avoid sorting X axis values. ?

2. All paramaters in single graph I would like to plot all the params in a single plot. Except X axis all other params values are between 0 to 1 (same scale) What will be best pythonic way of doing. Any help would be appreciated.

2
  • So you want to draw everything against (the reverse order of) col0? Commented May 28, 2019 at 18:27
  • 1
    Try inverting the x-axis using plt.gca().invert_xaxis() and see if this is what you want Commented May 28, 2019 at 18:28

2 Answers 2

1

Try to draw the series/dataframe against the index:

col_to_draw = [col for col in df.columns if col!='col0']

# if your data frame is indexed as 0,1,2,... ignore this step
tmp_df = df.reset_index()

ax = tmp_df[col_to_draw].plot(figsize=(10,6))
xtick_vals = ax.get_xticks()
ax.set_xticklabels(tmp_df.col0[xtick_vals].tolist())

Output:

enter image description here

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

Comments

1

I don't understand what you mean by they get sorted - does it not plot 112, 0.90178 and connect it to 110.89899, 0.90779, etc?

To share the X axis but have 2 Y axes that certain sets are plotted on, use twinx

fig, ax1 = plt.subplots()
ax1.plot(df.col_0, df.col_1)
ax2 = ax1.twinx()
ax2.plot(df.col_0, df.col_2)

re: how to plot in the order you want

I believe your intention is to actually plot these values vs. time or index. To that end, I suggest:

fig, ax1 = plt.subplots()
ax1.plot(df['Time'], df.col_0) # or df.index, df.col_0
ax2 = ax1.twinx()
ax2.plot(df['Time'], df.col_1)

2 Comments

sorted : it plotted in ascending order. Like My data starts from 112 till 0 but it gets plotted from 0 to 112 in x axis. (Refer picture in my question)
Are you saying you want the axis to be reversed? Or that you actually want to plot time vs. speed?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.