Matplotlib标签未正确填充。

huangapple go评论135阅读模式
英文:

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&#39;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&#39;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 &#39;Albania&#39;, 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[&#39;Year&#39;])

This will set all your x-tick labels to a range of years

</details>



huangapple
  • 本文由 发表于 2023年3月12日 19:23:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定