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

pstatix
  • 3,611
  • 4
  • 18
  • 40
  • The X axis values are already there... in scientific notation. Would you rather have the actual standard form? – J. Linne Apr 17 '20 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. – pstatix Apr 17 '20 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? – MachineLearner Apr 17 '20 at 21:23
  • https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.xticks.html ? – J. Linne Apr 17 '20 at 21:26
  • Does this answer your question? [Python Pandas: How to set Dataframe Column value as X-axis labels](https://stackoverflow.com/questions/38683709/python-pandas-how-to-set-dataframe-column-value-as-x-axis-labels). You would do `df.plot(..., xticks=df.index)`. – mcskinner Apr 17 '20 at 21:26
  • @MachineLearner Not sure if I want any type of scaling on the axis. I just want the x ticks for a 1-to-1 relationship with the index of the dataframe. Notice how there are roughly 7 points clustered on the left of the graph? I would like each point to be represented by a single tick of 10 ticks along the X axis. – pstatix Apr 17 '20 at 21:34
  • @mcskinner That works, however scaling is still in scientific notation. – pstatix Apr 17 '20 at 21:35
  • This [Q&A on formatting the axis ticks](https://stackoverflow.com/questions/44496383/format-x-axis-on-chart-created-with-pandas-plot-method) should help. – mcskinner Apr 17 '20 at 21:36
  • Your index is the first colum or do you want 0, 1, 2, ... as index? – MachineLearner Apr 17 '20 at 21:37
  • @MachineLearner I want the x axis to be 100, 1000, 10000, etc. But not scaled, such that the lower values are close together (as shown), I want them spread out just across the 10 ticks representing the 10 values. – pstatix Apr 17 '20 at 21:39
  • @MachineLearner Please see update with graph – pstatix Apr 17 '20 at 21:52
  • @mcskinner Please see update with graph – pstatix Apr 17 '20 at 21:52

2 Answers2

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

mcskinner
  • 2,620
  • 1
  • 11
  • 21
0
df.columns = ['xaxis', 'A', 'B']
numbers = np.arange(0, 900)
df.plot(x='xaxis', y=numbers, colormap='jet')
plt.show()
Neuron
  • 5,141
  • 5
  • 38
  • 59
Sammiti Yadav
  • 78
  • 1
  • 6
  • While this code may solve the question, [including an explanation](https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers) 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](https://stackoverflow.com/posts/70583748/edit) your answer to add explanations and give an indication of what limitations and assumptions apply. – mischva11 Jan 06 '22 at 09:48