gpt4 book ai didi

python - 具有相同颜色和标记的散点图子图

转载 作者:太空宇宙 更新时间:2023-11-03 15:12:19 25 4
gpt4 key购买 nike

我正在创建一个包含两个带散点图的子图的图形。我想为每个子图使用相同的配色方案和标记定义,但似乎无法让它工作。请原谅我的最小工作示例的长度,但我已尽可能地缩减了它。

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr
from scipy.stats import probplot

#Raw Data
area_old = [7603.4897489697905, 2941.7094279413577, 8153.896678990219, 7289.99097646249, 8620.196237363853, 11619.546945954673, 8458.80648310436, 7161.530990460888, 28486.298572761007, 4928.4856128268875, 4219.122621992603, 31687.155529782176]

combined = [7603.4897489697905, 2941.7094279413577, 8153.896678990219, 7289.99097646249, 8620.196237363853, 11619.546945954673, 8458.80648310436, 7161.530990460888, 28486.298572761007, 4928.4856128268875, 4219.122621992603, 31687.155529782176, 3059.4357099599456, 3348.0415691055823, 4839.023360449559, 4398.877634354169, 29269.67455441528, 11058.400909555028, 18266.34679952683, 16641.3446048029, 24983.586163502885, 5811.868753338233]

#Attributes to map colors and markers to
lt_bt = ['r','s','s','r','r','u','r','s','r','r','s','r']
combined_bt =['r','s','s','r','r','u','r','s','r','r','s','r','u','u','r','s','r','s','r','r','r','u']

#Get Probability plot Data
a = probplot(area_old,dist='norm', plot=None)
b= probplot(combined,dist='norm', plot=None)

#Colors and Markers to use
colors = {'r':'red','s':'blue', 'u':'green'}
markers = {'r':'*','s':'x', 'u':'o'}

#Create Dataframe to combine raw data, attributes and sort
old_df = pd.DataFrame(area_old, columns=['Long Term Sites: N=12'])
old_df['Bar_Type'] = lt_bt
old_df = old_df.sort_values(by='Long Term Sites: N=12')
old_df['quart']=a[0][0]

#Pandas series of colors for plotting on subplot 'ax'
ax_color = old_df.loc[:,'Bar_Type'].apply(lambda x: colors[x])

#Create Dataframe to combine raw data, attributes and sort
combined_df = pd.DataFrame(combined, columns=['ALL SITES N=22'])
combined_df['Bar_Type'] = combined_bt
combined_df = combined_df.sort_values(by='ALL SITES N=22')
combined_df['quart']=b[0][0]

#Pandas series of colors for plotting on subplot 'ax1'
ax1_color = combined_df.loc[:,'Bar_Type'].apply(lambda x: colors[x])

#Legend Handles
undif = plt.Line2D([0,0],[0,1], color='green',marker='o',linestyle=' ')
reatt = plt.Line2D([0,0],[0,1], color='red',marker='*',linestyle=' ')
sep = plt.Line2D([0,0],[0,1], color='blue',marker='x',linestyle=' ')


fig,(ax,ax1) = plt.subplots(ncols=2,sharey=True)

#Plot each data point seperatly with different markers and colors
for i, thing in old_df.iterrows():
ax.scatter(thing['quart'],thing['Long Term Sites: N=12'],c=ax_color.iloc[i],marker=markers[thing['Bar_Type']],zorder=10,s=50)
del i, thing

for i , thing in combined_df.iterrows():
ax1.scatter(thing['quart'],thing['ALL SITES N=22'],c=ax1_color.iloc[i],marker=markers[thing['Bar_Type']],zorder=10,s=50)
del i, thing

ax.set_title('LONG TERM SITES N=12')
ax1.set_title('ALL SITES N=22')
ax1.set_ylabel('')
ax.set_ylabel('TOTAL EDDY AREA, IN METERS SQUARED')
ax.set_ylim(0,35000)
ax.get_yaxis().set_major_formatter(tkr.FuncFormatter(lambda x, p: format(int(x), ',')))

legend = ax.legend([reatt,sep,undif],["Reattachment","Separation", "Undifferentiated"],loc=2,title='Bar Type',fontsize='x-small')

plt.setp(legend.get_title(),fontsize='x-small')

ax.set_xlabel('QUANTILES')
ax1.set_xlabel('QUANTILES')
plt.tight_layout()

基本思想是我逐点绘制散点图,以分配适当的颜色和标记。我使用 pandas 整数索引 .iloc 分配颜色,并通过为 markers 字典指定键来分配标记。

我知道有些事情不对,因为 old_dfcombined_df 中的第一点(即 old_df.loc[1,:]combined_df.loc[1,:]) 的颜色和标记应分别为 'blue''x'

我做错了什么?

最佳答案

不知道为什么,但不知何故在 ax.scatter 中使用 .iloc 会导致不可预测的行为。我所要做的就是删除 .iloc 方法并将其替换为字典映射(即 c=ax_color.iloc[i]c=colors[ thing['Bar_Type']])一切正常!

所需结果的工作示例:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr
from scipy.stats import probplot

#Raw Data
area_old = [7603.4897489697905, 2941.7094279413577, 8153.896678990219, 7289.99097646249, 8620.196237363853, 11619.546945954673, 8458.80648310436, 7161.530990460888, 28486.298572761007, 4928.4856128268875, 4219.122621992603, 31687.155529782176]

combined = [7603.4897489697905, 2941.7094279413577, 8153.896678990219, 7289.99097646249, 8620.196237363853, 11619.546945954673, 8458.80648310436, 7161.530990460888, 28486.298572761007, 4928.4856128268875, 4219.122621992603, 31687.155529782176, 3059.4357099599456, 3348.0415691055823, 4839.023360449559, 4398.877634354169, 29269.67455441528, 11058.400909555028, 18266.34679952683, 16641.3446048029, 24983.586163502885, 5811.868753338233]

#Attributes to map colors and markers to
lt_bt = ['r','s','s','r','r','u','r','s','r','r','s','r']
combined_bt =['r','s','s','r','r','u','r','s','r','r','s','r','u','u','r','s','r','s','r','r','r','u']

#Get Probability plot Data
a = probplot(area_old,dist='norm', plot=None)
b= probplot(combined,dist='norm', plot=None)

#Colors and Markers to use
colors = {'r':'red','s':'blue', 'u':'green'}
markers = {'r':'*','s':'x', 'u':'o'}

#Create Dataframe to combine raw data, attributes and sort
old_df = pd.DataFrame(area_old, columns=['Long Term Sites: N=12'])
old_df['Bar_Type'] = lt_bt
old_df = old_df.sort_values(by='Long Term Sites: N=12')
old_df['quart']=a[0][0]

#Pandas series of colors for plotting on subplot 'ax'
ax_color = old_df.loc[:,'Bar_Type'].apply(lambda x: colors[x])

#Create Dataframe to combine raw data, attributes and sort
combined_df = pd.DataFrame(combined, columns=['ALL SITES N=22'])
combined_df['Bar_Type'] = combined_bt
combined_df = combined_df.sort_values(by='ALL SITES N=22')
combined_df['quart']=b[0][0]

#Pandas series of colors for plotting on subplot 'ax1'
ax1_color = combined_df.loc[:,'Bar_Type'].apply(lambda x: colors[x])

#Legend Handles
undif = plt.Line2D([0,0],[0,1], color='green',marker='o',linestyle=' ')
reatt = plt.Line2D([0,0],[0,1], color='red',marker='*',linestyle=' ')
sep = plt.Line2D([0,0],[0,1], color='blue',marker='x',linestyle=' ')


fig,(ax,ax1) = plt.subplots(ncols=2,sharey=True)

#Plot each data point seperatly with different markers and colors
for i, thing in old_df.iterrows():
ax.scatter(thing['quart'],thing['Long Term Sites: N=12'],c=colors[thing['Bar_Type']],marker=markers[thing['Bar_Type']],zorder=10,s=50)
del i, thing

for i , thing in combined_df.iterrows():
ax1.scatter(thing['quart'],thing['ALL SITES N=22'],c=colors[thing['Bar_Type']],marker=markers[thing['Bar_Type']],zorder=10,s=50)
del i, thing

ax.set_title('LONG TERM SITES N=12')
ax1.set_title('ALL SITES N=22')
ax1.set_ylabel('')
ax.set_ylabel('TOTAL EDDY AREA, IN METERS SQUARED')
ax.set_ylim(0,35000)
ax.get_yaxis().set_major_formatter(tkr.FuncFormatter(lambda x, p: format(int(x), ',')))

legend = ax.legend([reatt,sep,undif],["Reattachment","Separation", "Undifferentiated"],loc=2,title='Bar Type',fontsize='x-small')

plt.setp(legend.get_title(),fontsize='x-small')

ax.set_xlabel('QUANTILES')
ax1.set_xlabel('QUANTILES')
plt.tight_layout()

关于python - 具有相同颜色和标记的散点图子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44121931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com