Matplotlib 问题:为什么 colorbar 会删除我的 xlabel?我该如何避免呢?
Matplotlib problem: why is a colorbar deleting my xlabel? And how can i avoid it?
我在做著名的房价数据集,遇到了一个问题
我的 xlabel 没有显示。我发现这是由绘制颜色条引起的。我想知道如何绘制颜色条并仍然保留我的 xlabel。
编辑:数据集可以用sklearn库加载:
from sklearn.datasets.california_housing import fetch_california_housing
housing = fetch_california_housing()
这是没有 xlabel 的代码:
housing.plot(kind='scatter', x = 'longitude', y= 'latitude', alpha = 0.2, figsize=(9,6),
s = housing['population']/100, cmap = plt.get_cmap('jet'),
label = 'population',
c = housing['median_house_value'],
colorbar=True,
title = 'Housing prices in relation to location and population density'
);
plt.legend();
带颜色条但不带 xlabel 的散点图:
这是带有 xlabel 的代码(但没有颜色条):
housing.plot(kind='scatter', x = 'longitude', y= 'latitude', alpha = 0.2, figsize=(9,6),
s = housing['population']/100, cmap = plt.get_cmap('jet'),
label = 'population',
c = housing['median_house_value'],
colorbar=False,
title = 'Housing prices in relation to location and population density'
);
plt.legend();
没有颜色条但有 xlabel 的散点图:
非常感谢任何提示或提示。
您需要在 df.plot()
调用中定义 matplotlib 轴对象,这是一个最小的示例:
import pandas as pd
import matplotlib.pyplot as plt
# Dummy data
iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
# Create a figure and get the axes object
fig, ax = plt.subplots()
# The plot
iris.plot(kind = 'scatter', # Random parameter
x = 'sepal_width',
y = 'petal_width',
alpha = 0.2,
figsize = (9,6),
cmap = plt.get_cmap('jet'),
s = iris['sepal_length']*10,
c = iris['sepal_length'],
colorbar= True,
ax = ax); # Pass the axes object !
其中给出:
而不是:
我在做著名的房价数据集,遇到了一个问题 我的 xlabel 没有显示。我发现这是由绘制颜色条引起的。我想知道如何绘制颜色条并仍然保留我的 xlabel。
编辑:数据集可以用sklearn库加载:
from sklearn.datasets.california_housing import fetch_california_housing
housing = fetch_california_housing()
这是没有 xlabel 的代码:
housing.plot(kind='scatter', x = 'longitude', y= 'latitude', alpha = 0.2, figsize=(9,6),
s = housing['population']/100, cmap = plt.get_cmap('jet'),
label = 'population',
c = housing['median_house_value'],
colorbar=True,
title = 'Housing prices in relation to location and population density'
);
plt.legend();
带颜色条但不带 xlabel 的散点图:
这是带有 xlabel 的代码(但没有颜色条):
housing.plot(kind='scatter', x = 'longitude', y= 'latitude', alpha = 0.2, figsize=(9,6),
s = housing['population']/100, cmap = plt.get_cmap('jet'),
label = 'population',
c = housing['median_house_value'],
colorbar=False,
title = 'Housing prices in relation to location and population density'
);
plt.legend();
没有颜色条但有 xlabel 的散点图:
非常感谢任何提示或提示。
您需要在 df.plot()
调用中定义 matplotlib 轴对象,这是一个最小的示例:
import pandas as pd
import matplotlib.pyplot as plt
# Dummy data
iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
# Create a figure and get the axes object
fig, ax = plt.subplots()
# The plot
iris.plot(kind = 'scatter', # Random parameter
x = 'sepal_width',
y = 'petal_width',
alpha = 0.2,
figsize = (9,6),
cmap = plt.get_cmap('jet'),
s = iris['sepal_length']*10,
c = iris['sepal_length'],
colorbar= True,
ax = ax); # Pass the axes object !
其中给出:
而不是: