python - matplotlib plt 未在散点图上正确设置 xlabel/ylabel
问题描述
我正在创建一个地理图并尝试设置我的 xlabels 和 ylabels。它只显示 ylabel,但不显示 xlabel。这是一个最小的可重现示例。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
d = {
'College': ['East Los Angeles College', 'Santa Monica Ccollege', 'American River College', 'Santa Ana College', 'Mount San Antonio College'],
'Total Enrollment': [36606, 29999, 29701, 28698, 28481],
'Latitude': [34.0414, 34.0166, 38.6511, 33.7580, 34.0482],
'Longitude': [-118.1503, -118.4704, -121.3467, -117.8889, 117.8451]
}
df = pd.DataFrame(data=d)
ax2 = df.plot.scatter(x='Longitude', y='Latitude',
s=df['Total Enrollment']/100, label='Enrollment',
c='Total Enrollment', cmap=plt.get_cmap("jet"),
colorbar=True, alpha=0.5, figsize=(15,12))
plt.ylabel("Latitude", fontsize=14)
plt.xlabel("Longitude", fontsize=14)
plt.legend(fontsize=16)
plt.show()
解决方案
添加以下几行似乎对我有用:
fig, ax = plt.subplots()
...
df.plot.scatter(..., ax=ax)
ax.set_ylabel("Latitude", fontsize=14)
ax.set_xlabel("Longitude", fontsize=14)
尽管如本问题所述,调用plt.subplots
和指定轴似乎是缺失的环节。
推荐阅读
- testing - alexa技能点出要发送物品的个人资料名称,城市和Pincode,这会通过认证吗?
- android - 图像文件从本地存储到 ImageView - android 10 存储权限问题
- python-2.7 - pyOpenSSL 中 crypto.X509 和 crypto.X509Req 之间的区别
- sql - 在 SQL Join 中删除全部或全部
- php - 无法通过 Amazon CloudFront 发送请求:403 错误
- r - 在 R 中形成一个尺寸为 100x100 的矩阵 (a^|ij|)
- python - 将多行和多列的文本连接成熊猫数据框中的单个文本字符串?
- angular - 如何将复杂的 JSON 数据绑定到剑道网格 Angular?
- reactjs - Gutenberg RichText 自定义块属性。“数组和子项”还是“字符串和 HTML”?
- javascript - 在反应中多次触发去抖动