0

I have a bar chart, the x-axis is (1,2,3...12). so my bar chart is something like this: enter image description here

how can I change:

1---> -6month 
2---> -1 year
3--->-1.5 year
.
.
.

while showing?

my code to plot is:

dffinal = df[['6month','final-formula','Question Text','numPatients6month']].drop_duplicates().sort_values(['6month'])

df = dffinal.drop('numPatients6month', 1).groupby(['6month','Question Text']).sum().unstack('Question Text')

df.columns = df.columns.droplevel()
ax=df.plot(kind='bar', stacked=True)
import matplotlib.pyplot as plt
ax2 = ax.twinx()
plt.xticks(fontsize=8, rotation=45)
#ax2.spines['right'].set_position(('axes', 1.0))
dffinal.plot(ax=ax2,x='6month', y='numPatients6month',visible=False)
plt.title('Cognitive Impairement-Stack bar')
plt.show()

I have two df as I have two y-axis.

I tried to use replace:

   dffinal['6month'].replace(1, '-6 month',inplace=True)
dffinal['6month'].replace(2, '-1 year',inplace=True)

but it just did not worked .

Thanks:)

4
  • Sorry but there's very little in common between your code and the plot you show :-) do you mean to just change the x ticks showing some text instead of 1,..., 12? If so, you just need to write a command like plt.xticks(range(1,13), ['-6month','-1 year',...], fontsize=8, rotation=90) instead of the plt.xticks you currently have. Your question title says y axis, but the y axis in this case doesn't make much sense with text / categorical variables... Commented Jun 21, 2018 at 15:59
  • @MarcoSpinaci thanks for your response I just edited the title. my mind was full withthe previous problem I had thats why you see some conflicts. I did not know I can add it in x.ticks now I am trying your code, then I will update Commented Jun 21, 2018 at 16:07
  • and about the conflict between plot and code you are right, I plotted one example I had and did not want run the code that has problem Commented Jun 21, 2018 at 16:09
  • @MarcoSpinaci it works perfectly, except it should be range(0,12) . please add your answer so I can choose it as the desired one :) Commented Jun 21, 2018 at 16:16

1 Answer 1

1

The command plt.xticks should take care of it. Depending on whether the counting of the x axis starts from 0 (as default) or from 1 (as your plot implies) you could try:

# If x starts from 0
plt.xticks(range(12), ['-6month','-1 year',...], fontsize=8, rotation=90)

or

# If x starts from 1
plt.xticks(range(1,13), ['-6month','-1 year',...], fontsize=8, rotation=90)

In both cases replacing ['-6month','-1 year',...] by the 12 elements list of the labels you want.

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

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.