Home > Net >  How to set the xtick to start from first bar not second?
How to set the xtick to start from first bar not second?

Time:10-23

I have the following code generating a graph using the following data

data

Point   Actual  Predicted
1   62.33   62.3368
2   64.9    64.97409
3   66.61   66.64898
4   69.92   69.91074
5   70.79   70.80359
6   71.41   71.42479
7   74.86   74.84357
8   78.36   78.31594
9   79.67   79.65846
10  80.43   80.40633

Code to generate the graph.

df = pd.DataFrame(data)

plt.figure(figsize=(3,2))

X = list(df.iloc[:, 0])
Y = list(df.iloc[:, 1])
Z= list(df.iloc[:, 2])

X_axis = np.arange(len(X))
plt.bar(X_axis - 0.2, Y, 0.4, label='Actual',color='#436bad')
plt.bar(X_axis   0.2, Z, 0.4, label='Predicted',color='#c5c9c7')
plt.legend(loc=2, prop={'size': 6.5})
labels=['1','2','3','4','5','6','7','8','9','10']
plt.xticks(X,labels,rotation=60)
plt.xlabel("points")
plt.ylabel("Accuracy (%)")
plt.ylim(60,90)

enter image description here

In the below image I need the xtick to start from the first bar but the tick starts from second bar.

CodePudding user response:

Here you go:

import matplotlib.pyplot as plt
import numpy as np

X = np.array([1,2,3,4,5,6,7,8,9,10])
Y = np.array([62.33,64.9 ,66.61,69.92,70.79,71.41,74.86,78.36,79.67,80.43])
Z = np.array([62.3368,64.97409,66.64898,69.91074,70.80359,71.42479,74.84357,78.31594,79.65846,80.40633])

plt.figure(figsize=(3,2))
plt.bar(X - 0.2, Y, 0.4, label='Actual',color='#436bad')
plt.bar(X   0.2, Z, 0.4, label='Predicted',color='#c5c9c7')
plt.legend(loc=2, prop={'size': 6.5})
labels=['1','2','3','4','5','6','7','8','9','10']
plt.xticks(X,labels,rotation=60)
plt.xlabel("points")
plt.ylabel("Accuracy (%)")
plt.ylim(60,90)
plt.show()
  • Related