0

I have the following dataframe:

            A                B
0    0.020336         0.017300
1    0.068807         0.042916
2    0.543051         0.261117
3    2.643319         1.208245
4    5.295754         2.425145
5   26.091538        11.936791
6   52.407236        23.968562
7  261.233778       119.663701
8  522.850657       239.565097
9  829.366556       378.151528

I would like to plot in on an XY chart where the X axis ticks are the index values and the Y axis ticks represent the range [0, 900]. The data in the columns would be plotted accordingly. When I currently plot using:

df.plot(lw=2, colormap='jet', marker='.', markersize=8)
plt.show()

I get the following:

Graph

The Y axis is fine, but the X axis appears to be showing a scaled range of the index values.

How can I make the X axis the actual values of the dataframe index? (the curves should start to appear parabolic this way).

Edit:

This would be the desired output, but with the correct number of ticks:

enter image description here

12
  • The X axis values are already there... in scientific notation. Would you rather have the actual standard form? Commented Apr 17, 2020 at 21:19
  • @J.Linne That notation alters the graphs true representation (and scale). I wish for the 10 ticks of the X axis to be represented by the 10 actual values. This graph should have a curve that appears parabolic. For example, the lower numbers are all "bunched" up at the beginning, I want them evenly spread out with each getting their own respective X-tick. Commented Apr 17, 2020 at 21:21
  • Your dependence seems to be linear and the plot has correct scientific format. E.g. 1.6e7=16000000. For linear relationship: If you double the x-value your y-value doubles. Maybe you want a logarithmic scaling on the x-axis? Commented Apr 17, 2020 at 21:23
  • matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.xticks.html ? Commented Apr 17, 2020 at 21:26
  • Does this answer your question? Python Pandas: How to set Dataframe Column value as X-axis labels. You would do df.plot(..., xticks=df.index). Commented Apr 17, 2020 at 21:26

2 Answers 2

0
# Save the index to use for labels.
x_values = df.index

# Reset the index to [0, N).
df = df.reset_index(drop=True)

# Plot the re-indexed data.
ax = df.plot(lw=2, colormap='jet', marker='.', markersize=8)

# Use the saved labels.
ax.set_xticklabels(x_values)

customized plot

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

5 Comments

The graph now plots correctly, but my labels don't match yours, doing exactly what you've written here. I only get about 5 ticks.
Can you print df for me and paste the formatted text in your question? I can help work with the sample data.
The dataframe is pasted in the OP now.
Managed to get it working, had to set xticks in the plot() call.
My mistake, I forgot to assign ax = df.plot(...). Can you check if this solution works for you?
0
df.columns = ['xaxis', 'A', 'B']
numbers = np.arange(0, 900)
df.plot(x='xaxis', y=numbers, colormap='jet')
plt.show()

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.

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.