我有以下代碼使用以下資料生成圖表
資料
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
生成圖形的代碼。
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)

在下圖中,我需要 xtick 從第一個條開始,但刻度從第二個條開始。
uj5u.com熱心網友回復:
干得好:
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335143.html
標籤:Python matplotlib
