我正在创建一个带有两个带有散点图的子图的图形。我想为每个子图使用相同的颜色方案和标记定义,但似乎无法使其工作。请原谅我最小的工作示例的长度,但我尽可能地削减它。
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_df
和combined_df
中的第一个点(即old_df.loc[1,:]
,combined_df.loc[1,:]
)应该具有{的颜色和标记分别为{1}}和'blue'
。
我做错了什么?
答案 0 :(得分:1)
不确定原因,但以某种方式在.iloc
中使用ax.scatter
会导致无法预测的行为。我所要做的就是删除.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()