1

I have a dataframe, the 'x' column is like [8, 9, 10, ..., 24, 1, 2, 3, ..., 7]. When I try to plot it, the x axis will still start from 1, 2, 3, ... Could I change it as start from 8 to 24, then 1 to 7? The code is as follow:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

l1 = [x for x in range(1, 25)]
l2 = l1[7:] + l1[:7]
arr1 = np.asarray(l2)
arr1

y = np.random.rand(24)

df = pd.DataFrame({'x': arr1, 'y': y})

fig = plt.figure()

ax = fig.add_subplot(111)

ax.bar(df['x'],df['y'])
ax.set_ylim(0, 1)

plt.show()

print(df)

     x         y
0    8  0.354536
1    9  0.418379
2   10  0.902957
3   11  0.026550
4   12  0.560771
5   13  0.804618
6   14  0.114657
7   15  0.969412
8   16  0.595874
9   17  0.193734
10  18  0.740406
11  19  0.848634
12  20  0.799882
13  21  0.674117
14  22  0.453562
15  23  0.009416
16  24  0.124332
17   1  0.232094
18   2  0.405055
19   3  0.034836
20   4  0.627928
21   5  0.347363
22   6  0.170759
23   7  0.084413
3
  • There are two options essentially. (1) Fake the axis units by plotting the index and setting the ticklabels to the shifted numbers. (2) Truely change the axis units. Not sure what you're after here. Commented Nov 9, 2018 at 0:55
  • think the first is good enough for me, I just want to start the unit from the middle (8 in this example) Commented Nov 9, 2018 at 1:01
  • I think I have to truly change the axis unit, just change the display of the ticklabels would shift the plotting in my case. Commented Nov 9, 2018 at 1:51

2 Answers 2

2

You simply need this:

ax.bar(np.arange(len(df)), df['y'])
ax.set_xticks(np.arange(len(df)))
ax.set_xticklabels(df['x'])
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks! @Julien it works great for me! But could I ask why "ax.set_xticklabels(df['x'])" would not work and have to add the line "ax.set_xticks(np.arange(len(df)))"?
This is to specify the location of your ticks. If you don't do it it will use default settings which will screw you up.
alright, got it now. thanks so much for your explanation!~
Hi @Julien I found a problem for this. It only change the display of the x axis, but not really changing the plot
Not sure what you mean... Why should it "change the plot"?
|
0

so after several try I figure it out the problem. My problem is my want to plot with discontinous axis unit(not break), the figure I want is the one at the bottom (from 5 to 10 then back from 1 to 4)

enter image description here

following is the code with the great help of @Julien

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#generate data for 'x' column
list_x = [x for x in range(1, 11)]
arrX = np.asarray(list_x)

#generate data for 'y' column
list_y = [i for i in range(10, 110, 10)]
arrY = np.asarray(list_y)

#generate the dataframes
df1 = pd.DataFrame({'x': arrX, 'y': arrY})

df2 = pd.concat([df1[4:],df1[:4]])
df2 = df2.reset_index(drop=True)

print(df1)
print(df2)

fig = plt.figure(dpi=128, figsize=(6, 6))
ax1, ax2 = fig.add_subplot(211), fig.add_subplot(212)

ax1.bar(np.arange(len(df1)), df1['y'])
ax1.set_xticks(np.arange(len(df1)))
ax1.set_xticklabels(df1['x'])

ax2.bar(np.arange(len(df2)), df2['y'])
# ax2.bar(df2['x'], df2['y'])  ## this will not work. Has to use the code above

# @Julien provides the following to modify the label
ax2.set_xticks(np.arange(len(df2)))
ax2.set_xticklabels(df2['x'])


plt.show()

Comments

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.