停止颜色在不同散点图实例之间重置

huangapple go评论71阅读模式
英文:

Stop color from resetting between different scatterplot instances

问题

import matplotlib.pyplot as plt
import pandas as pd

# 创建数据点
data1 = [i for i in range(100)]
data2 = [i for i in range(100)]
data3 = [i for i in range(100)]

# 创建标签
label1 = [1 for _ in range(50)] + [2 for _ in range(50)]
label2 = [3 for _ in range(50)] + [4 for _ in range(50)]
label3 = [5 for _ in range(50)] + [6 for _ in range(50)]

# 合并为数据框
df = pd.DataFrame({'data1': data1, 'data2': data2, 'data3': data3, 'label1': label1, 'label2': label2, 'label3': label3})

# 创建图表
plt.figure()
plt.scatter(df['data1'], [0 for _ in range(df['data1'].shape[0])], c=df['label1'], cmap='viridis')
plt.scatter(df['data2'], [1 for _ in range(df['data2'].shape[0])], c=df['label2'], cmap='viridis')
plt.scatter(df['data3'], [2 for _ in range(df['data3'].shape[0])], c=df['label3'], cmap='viridis')
plt.yticks(ticks=[0, 1, 2], labels=['data1', 'data2', 'data3'])
plt.show()
英文:

I'm making 1D plots of a number of data columns using scatter plots. Each instance in each column has a label which represents some atribute. In the 1D plot this label is displayed using a colour.

It's something as follows:

import matplotlib.pyplot as plt
import pandas as pd

# make data points
data1 = [i for i in range(100)]
data2 = [i for i in range(100)]
data3 = [i for i in range(100)]

# make labels
label1 = [1 for _ in range(50)] + [2 for _ in range(50)]
label2 = [3 for _ in range(50)] + [4 for _ in range(50)]
label3 = [5 for _ in range(50)] + [6 for _ in range(50)]

# combine into dataframe
df = pd.DataFrame({'data1':data1, 'data2':data2, 'data3':data3, 'label1':label1, 'label2':label2, 'label3':label3})

# make figure
plt.figure()
plt.scatter(df['data1'], [0 for _ in range(df['data1'].shape[0])], c=df['label1'])
plt.scatter(df['data2'], [1 for _ in range(df['data2'].shape[0])], c=df['label2'])
plt.scatter(df['data3'], [2 for _ in range(df['data3'].shape[0])], c=df['label3'])
plt.yticks(ticks=[0,1,2], labels=['data1', 'data2', 'data3'])
plt.show()

Note, each data column has its own label column and the labels are different. For data1 this is 1,2 for data2 3,3 and for data3 5,6. When I make my 1D plots here each of the points get the same colors. So for data1 labels 1 and 2 are the same color as labels 3 and 4 in data2 etc.

My question is how can I make each label have its own color in the plot such that 1,3,5 and 2,4,6 do not have the same color?

答案1

得分: 1

将代码中的注释和代码片段翻译如下:

# 将 `vmin, vmax` 传递给 `scatter` 函数:

# vmin, vmax 可以通过以下方式获得:
# vmin, vmax = df.filter(like='label').stack().agg(['min', 'max'])

# 创建图形
plt.figure()
plt.scatter(df['data1'], [0] * len(df['data1']), c=df['label1'], vmin=1, vmax=6)
plt.scatter(df['data2'], [1] * len(df['data2']), c=df['label2'], vmin=1, vmax=6)
plt.scatter(df['data3'], [2] * len(df['data3']), c=df['label3'], vmin=1, vmax=6)
plt.yticks(ticks=[0, 1, 2], labels=['data1', 'data2', 'data3'])
plt.show()

输出:

停止颜色在不同散点图实例之间重置

英文:

Pass vmin, vmax to scatter:

# vmin, vmax can be obtained by
# vmin, vmax = df.filter(like='label').stack().agg(['min','max'])

# make figure
plt.figure()
plt.scatter(df['data1'], [0] * len(df['data1']), c=df['label1'], vmin=1, vmax=6)
plt.scatter(df['data2'], [1] * len(df['data2']), c=df['label2'], vmin=1, vmax=6)
plt.scatter(df['data3'], [2] * len(df['data3']), c=df['label3'], vmin=1, vmax=6)
plt.yticks(ticks=[0,1,2], labels=['data1', 'data2', 'data3'])
plt.show()

Output:

停止颜色在不同散点图实例之间重置

huangapple
  • 本文由 发表于 2023年4月19日 23:24:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定