简体   繁体   English

尝试向 Seaborn 散点图添加颜色条

[英]Trying to add a colorbar to a Seaborn scatterplot

I'm a geology master's student working on my dissertation with a focus on the Sulfur Dioxide output of a number of volcanoes in the South Pacific.我是一名地质学硕士生,正在写我的论文,重点是南太平洋一些火山的二氧化硫 output。 I have a little experience with R but my supervisor recommended python (JupyterLab specifically) for generating figures and data manipulation so I'm pretty new to programming and essentially teaching myself as I go.我对 R 有一点经验,但我的主管推荐 python(特别是 JupyterLab)用于生成数字和数据操作,所以我对编程和基本上自学 Z34D1F91FB2E514B8576FAB1A75A89A 非常陌生。 I'm trying to use earthquake data to generate some scatterplots using seaborn but I can't seem to get a color bar to show up in the legend for the earthquake magnitude.我正在尝试使用地震数据使用 seaborn 生成一些散点图,但我似乎无法在地震震级的图例中显示颜色条。 The code I'm using is below and I'll do my best to format it in a clear way.我正在使用的代码如下,我会尽力以清晰的方式对其进行格式化。

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
from scipy import stats
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt

then the data sets I'm working with.然后是我正在使用的数据集。 These are the sets for Earthquake data.这些是地震数据集。

df = pd.read_csv('Vanuatu Earthquakes May18-May19.csv')
df = pd.read_csv('Vanuatu Earthquakes May17-May18.csv')
df = pd.read_csv('Vanuatu Earthquakes May19-Jul20.csv')

and locations of the volcanoes, purely there for spatial reference.和火山的位置,纯粹是为了空间参考。

dg = pd.read_csv('Volcano coordinates.csv')

Here's the main plot I'm trying to work with as it stands at the moment.这是我目前正在尝试使用的主要 plot。 So far I've been able to classify the earthquakes' magnitudes using the hue function but I don't like how it looks in the legend and want to convert it to a colorbar (or use a colorbar instead of hue, either/or), except I can't quite figure out how to do that.到目前为止,我已经能够使用色调 function 对地震的震级进行分类,但我不喜欢它在图例中的外观,并且想将其转换为颜色条(或使用颜色条而不是色调,或者/或) ,但我不知道该怎么做。 Alternatively, if there's a different function that would give me the results I'm looking for, I'm definitely open to that instead of a scatterplot.或者,如果有一个不同的 function 会给我我正在寻找的结果,我肯定会接受它而不是散点图。 Also the black triangles are the volcanoes so those can be ignored for now.黑色三角形也是火山,所以现在可以忽略它们。

plt.figure(figsize=(5.5,9))
sns.scatterplot(x='longitude', y='latitude', data=df, 
                marker='D', hue='mag', palette='colorblind', cmap='RdBu')
sns.scatterplot(x='longitude', y='latitude', data=dg, 
                marker='^', legend='brief', color='k', s=100)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., title='Magnitude (Mw)')
plt.xlabel('Longitude (degrees)')
plt.ylabel('Latitude (degrees)')
plt.title('Earthquake and Volcano Locations', size=15)
plt.show()

图片

Hopefully that's clear enough but let me know if more info is needed!希望这足够清楚,但如果需要更多信息,请告诉我!

The same method employed in this answer regarding Seaborn barplots can be applied to a scatterplot as well.此答案中使用的关于 Seaborn 条形图的相同方法也可以应用于散点图。 With your code that would look something like this:使用您的代码,看起来像这样:

# ...
norm = plt.Normalize(df['mag'].min(), df['mag'].max())
sm = plt.cm.ScalarMappable(cmap="RdBu", norm=norm)
sm.set_array([])

ax = sns.scatterplot(x='longitude', y='latitude', data=df, marker='D', palette='RdBu', hue='mag')
sns.scatterplot(x='longitude', y='latitude', data=dg, marker='^', 
                legend='brief', color='k', s=100, ax=ax)

# Remove the legend and add a colorbar (optional)
# ax.get_legend().remove()
# ax.figure.colorbar(sm)

# ...

See this question and its answers for information on manipulating the labels and ticks of the color bar.有关操作颜色条的标签和刻度的信息,请参阅此问题及其答案。

For a complete example using the tips dataset:有关使用tips数据集的完整示例:

import seaborn as sns
import matplotlib.pyplot as plt

sns.set()
tips = sns.load_dataset("tips")
ax = sns.scatterplot(x="total_bill", y="tip", hue="size", palette='RdBu', data=tips)

norm = plt.Normalize(tips['size'].min(), tips['size'].max())
sm = plt.cm.ScalarMappable(cmap="RdBu", norm=norm)
sm.set_array([])

# Remove the legend and add a colorbar
ax.get_legend().remove()
ax.figure.colorbar(sm)

plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM