英文:
Matplotlib labels aren't filled correctly
问题
以下是翻译好的代码部分:
我有一个包含国家、年份和数值的数据框。
我已经为该数据设置了一个索引:
```python
data = data.set_index(data['Country or Area'])
data.drop(['Country or Area'], axis=1, inplace=True)
data.head()
我尝试创建一个类似这样的简单图表:
%matplotlib inline
albania = data.loc['Albania']
ax = albania[['Year', 'Value']].plot(kind='bar', figsize=(8,5), title="Albania 1990年至2015年的降水量")
ax.set_ylabel("降水量")
ax.set_xlabel("年份")
然而,条形图显示的y轴标签是'Albania'的索引名称,这是不正确的。它应该显示年份范围。
如有其他需要,请随时提问。
<details>
<summary>英文:</summary>
I have a dataframe with list of countries, years and values.
[![enter image description here][1]][1]
I've set an index for that data:
data = data.set_index(data['Country or Area'])
data.drop(['Country or Area'], axis=1, inplace=True)
data.head()
I'm trying to create a simple plot like this:
%matplotlib inline
albania = data.loc['Albania']
ax = albania[['Year','Value']].plot(kind='bar', figsize=(8,5), title="Precipitation of Albania between 1990 and 2015")
ax.set_ylabel("Precipitation")
ax.set_xlabel("Years")
However, the bar shows y_labels with the index name 'Albania', which is not correct. It should show the range of years.
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/D7Yzq.png
[2]: https://i.stack.imgur.com/eb20D.png
What should I do to change the labels to the correct one?
</details>
# 答案1
**得分**: 1
ax.set_xticklabels(albania['Year']) 这将把所有的x轴刻度标签设置为一系列年份。
<details>
<summary>英文:</summary>
ax.set_xticklabels(albania['Year'])
This will set all your x-tick labels to a range of years
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论