为什么我要绘制的代码没有显示标题为xlabel和ylabel的输出?

2024-10-02 18:15:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我曾尝试使用matplotlib绘制条形图,但在编写代码以显示标题和xlabel ylabel时,它们没有显示。我做错什么了?你知道吗

我已经创建了一个数据帧,并尝试使用df.plot(...)绘制相同的数据帧。 但是我没有得到标签选项和xticks。所以我试了这个。你知道吗

# Make a fake dataset:
districts = ['Alappuzha','Ernakulam','Idukki','Kannur','Kasaragod','Kollam','Kottayam','Kozhikode','Malappuram','Palakkad','Pathanamthitta','Thiruvananthapuram','Wayanad','Thrissur']
distAb = ['Alp','Ern','Idk','Knr','Ksd','Klm','Ktm','Kzd','Mlp','Plk','Pta','Tvm','Wyd','Tsr']
population = [2121943,3279860,1107453,2525637,1302600,2629703,1979384,3089543,4110956,2810892,1195537,3307284,816558,3110327]
posArrange = np.arange(len(districts))

创建条形图

plt.bar(posArrange,population,color='#ff9900', width=.8)


plt.title='Population of Districts in Kerala'
plt.xlabel='Districts'
plt.ylabel='Population'
plt.xticks(posArrange,distAb)

显示图形

plt.show()

输出不是我所期望的。它没有显示xlabel、ylabel和标题。 enter image description here


Tags: 数据标题matplotlib绘制plt条形图populationxlabel
2条回答

我已经在我的项目中使用了下面的代码,它正在工作。你可以这样用

import matplotlib.pyplot as plt 
left = [1, 2, 3, 4]
height = [logistic_score*100,naive_score*100,svm_score*100,mlp_score*100] 
tick_label = ['Logistic Regression', 'Naive Bayes', 'SVM', 'Multi linear 
perceptron'] 
plt.bar(left, height, tick_label = tick_label, width = 0.3, color = ['red', 
'green','blue','violet'])
plt.xlabel('Algorithms')
plt.ylabel('Efficiency')
plt.title('Results Comaprision') 
plt.show()

titlexlabelylabelxticks是函数,这意味着它们后面有带参数的方括号。 相应地更改以下行,您应该可以在绘图中看到所需的元素。你知道吗

plt.title('Population of Districts in Kerala')
plt.xlabel('Districts')
plt.ylabel('Population')
plt.xticks(posArrange,distAb)
plt.show()

相关问题 更多 >