旋转Xticks在海洋散点图中
我有一个要可视化的聚合数据集,看起来像:
,我需要绘制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'
那么我如何旋转这些标签(图中有状态名称,以便我没有收到错误并获得视觉上不错的绘图)?带有状态名称的列称为“状态”,因为它显然是从绘图代码的
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要更改tick参数,例如旋转,请使用 set_tick_params 而不是与旋转一起重新设置标签:
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:感谢 Stef 我们发现我试图使用的列的名称实际上是一个索引,所以我已经是一个索引修改了该行以在索引中查找这些状态名称,并有效:
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: