旋转Xticks在海洋散点图中

发布于 2025-02-10 19:58:12 字数 1485 浏览 0 评论 0 原文

我有一个要可视化的聚合数据集,看起来像:

“在此处输入图像描述”

,我需要绘制18个州的一些统计信息。目前,该情节以以下方式看起来:

”在此处输入图像描述“

,我设法将Xticks设置为使用以下代码,但是没有旋转,我会遇到错误。图的代码是:

fig, ax = plt.subplots(figsize = (15, 6))
sns.scatterplot(ax = ax, x = 'state', y = 'price per acre, usd', data = data)
ax.set_xlabel("state", size = 12)
ax.set_ylabel('average price per acre of land, usd', size = 12)
ax.set_title('average prices on industrial land', size = 20)
ax.set_xticklabels(data['state'], rotation = 45)
plt.show()

我会出现的错误:

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:
-> 3363                 raise KeyError(key) from err
   3364 
   3365         if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: 'state'

那么我如何旋转这些标签(图中有状态名称,以便我没有收到错误并获得视觉上不错的绘图)?带有状态名称的列称为“状态”,因为它显然是从绘图代码的

i have an aggregate dataset that i am trying to visualise, it looks like that:

enter image description here

and i need to plot some statistics for 18 states. currently the plot looks in the following way:

enter image description here

and i manage to set xticks with the following code, however there is no rotate and i get an error. the code for the plot is:

fig, ax = plt.subplots(figsize = (15, 6))
sns.scatterplot(ax = ax, x = 'state', y = 'price per acre, usd', data = data)
ax.set_xlabel("state", size = 12)
ax.set_ylabel('average price per acre of land, usd', size = 12)
ax.set_title('average prices on industrial land', size = 20)
ax.set_xticklabels(data['state'], rotation = 45)
plt.show()

and the error i get looks like this:

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:
-> 3363                 raise KeyError(key) from err
   3364 
   3365         if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: 'state'

so how i can rotate those labels (with names of states in the plot so that i do not receive an error and got a visually nice plot)? the column with the names of the state is called "state" as it is clearly from the plot code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等往事风中吹 2025-02-17 19:58:13

如果要更改tick参数,例如旋转,请使用 set_tick_params 而不是与旋转一起重新设置标签:

ax.xaxis.set_tick_params(rotation = 45)

If you want to change the tick parameters, e.g. the rotation, use set_tick_params instead of re-setting the labels along with the rotation:

ax.xaxis.set_tick_params(rotation = 45)
流殇 2025-02-17 19:58:13

感谢 Stef 我们发现我试图使用的列的名称实际上是一个索引,所以我已经是一个索引修改了该行以在索引中查找这些状态名称,并有效:

ax.set_xticklabels(data.index, rotation = 45)

thanks to stef we found out that the name of the column i was trying to use was actually an index, so i've modified the line to look for those state names in the index and it worked:

ax.set_xticklabels(data.index, rotation = 45)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文