python - 具有不同组的散点图和每组的边缘直方图
问题描述
我已经有了一个散点图,其中包含不同的元素组和边缘的直方图,但它们与整个数据相关联,而不是与各个组相关联:
我想要 2 个直方图,每组元素一个。我怎么做?这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import csv
from matplotlib.colors import LinearSegmentedColormap
data= pd.read_csv("data.csv")
x=data['Fe']
y=data['V']
z=data['Discovery']
# Fixing random state for reproducibility
np.random.seed(19680801)
# definitions for the axes
left, width = 0.1, 0.7
bottom, height = 0.1, 0.7
spacing = 0.05
rect_scatter = [left, bottom, width, height]
rect_histx = [left, bottom + height + spacing, width, 0.2]
rect_histy = [left + width + spacing, bottom, 0.2, height]
# start with a rectangular Figure
fig=plt.figure(figsize=(7, 6))
ax_scatter = plt.axes(rect_scatter)
ax_scatter.tick_params(direction='in', top=True, right=True)
ax_histx = plt.axes(rect_histx)
ax_histx.tick_params(direction='in', labelbottom=True)
ax_histy = plt.axes(rect_histy)
ax_histy.tick_params(direction='in', labelleft=False)
# the function that separates the dots in different classes:
classes = np.zeros( len(x) )
classes[(z == 'Transit')] = 1
classes[(z == 'Radial Velocity')] = 2
# create color map:
colors = ['purple', 'orange']
cm = LinearSegmentedColormap.from_list('custom', colors, N=len(colors))
# the scatter plot:
scatter = ax_scatter.scatter(x, y, c=classes, s=10, cmap=cm, alpha=0.6)
lines, labels = scatter.legend_elements()
# legend with custom labels
labels = [r'Transit', r'Radial Velocity']
legend = ax_scatter.legend(lines, labels,
loc="upper left", title="Planetary Discovery Method")
ax_scatter.add_artist(legend)
# now determine nice limits by hand:
binwidth = 0.1
ax_scatter.set_xlim((-1, 0.7))
ax_scatter.set_ylim((-0.9, 0.9))
#histogram
weights = np.ones_like(x)/(len(x))
weights2 = np.ones_like(y)/(len(y))
ax_histx.hist(x, bins=bins, weights=weights, color='chartreuse')
ax_histy.hist(y, bins=bins, weights=weights, orientation='horizontal', color='darkmagenta')
ax_histx.set_xlim(ax_scatter.get_xlim())
ax_histy.set_ylim(ax_scatter.get_ylim())
#labeling
ax_scatter.set_xlabel('[Fe/H]')
ax_scatter.set_ylabel('[V/H]')
ax_histy.set_xlabel('Relative Dist.')
ax_histx.set_ylabel('Relative Dist.')
plt.show()
我将添加一个我试图达到的情节示例:
解决方案
推荐阅读
- applescript - AppleScript,在解析大型 XML 文件时收到“系统事件出错:连接无效。(-609)”
- scala - 在 Azure Blob 存储中编写 Parquet:“其中一个请求输入无效”
- c# - C# entityframework 6 修改children状态后如何保存children
- node.js - 图像未在 node.js 服务器 ubuntu 20,aws 的前端 react.js 上呈现
- python - 安装具有相应旧依赖项的旧 python 模块
- javascript - 用于递增的构造函数问题(反应 Javascript)
- r - 如何按班级创建条形图ro显示中位数
- react-native - 从 Async Storage 获取 i18n 语言
- google-sheets - 谷歌表格“COUNTIF”功能不显示多个条件
- vba - 在 MS access vba 中使用 Selenium 进行数据提取