Python:将两个不同的图组合在一个图中但不同的x值范围
Python: Combining two different plots in one plot but different x value range
我想使用 seaborn 将点图和箱线图组合成一个图。该组合本身有效,但由于两个数据帧的 x 轴值不在同一范围内,我得到错误的输出(第二个数据帧的 x 值将被忽略)。
以下是我的部分数据集:
import pandas as pd
bpdata=pd.DataFrame({'Ldate': [20150202, 20150202, 20150202, 20151117, 20151117, 20151117, 20160205, 20160205, 20160205], 'Lreflectance': [0.067, 0.0482, 0.0555, 0.099, 0.0956, 0.0931, 0.0757, 0.0663, 0.0566]})
Mplotdata=pd.DataFrame({'Mdate':[20150202, 20150407,20160202], 'Mreflectance': [0.0868, 0.0833,0.0719]})
个人 Plottet:
import seaborn as sns
plot=sns.pointplot(data=Mplotdata, x='Mdate', y='Mreflectance', color='dimgrey', marker='o')
for item in plot.get_xticklabels():
item.set_rotation(45)
plot=sns.boxplot(data=bpdata, x='Ldate', y='Lreflectance', color='lightgray')
for item in plot.get_xticklabels():
item.set_rotation(45)
看起来像这样:
pointplot (left) and boxplot (right)
但是当我尝试将点图和箱线图都放在一个图中时
plot=sns.boxplot(data=bpdata, x='Ldate', y='Lreflectance', color='lightgray')
plot=sns.pointplot(data=Mplotdata, x='Mdate', y='Mreflectance', color='dimgrey', marker='o')
for item in plot.get_xticklabels():
item.set_rotation(45)
看起来像这样:
在此图中,x 轴仅显示第一个图的值。如果我更改点图和箱线图的顺序,它也将只使用第一个数据帧的 x 值,第二个将被忽略。
知道如何解决这个问题吗?
我还没有在这个论坛、google 或 matplotlib/seaborn 文档中找到任何解决方案。
fig, ax = plt.subplots()
ax2 = ax.twiny() # put a second plot on top of it, having the same y axis
data1 = np.random.random(10) #random in [0,1]
data2 = np.random.random(15)+1 #random in [1,2]
ax.plot(np.arange(len(data1)), data1) # plot in the first plot
ax2.plot(np.arange(len(data2)),data2) # plot in the second plot
谢谢@JürgMerlinSpaak,就是这样:
fig, ax = plt.subplots()
ax2 = ax.twiny()
plot=sns.boxplot(data=bpdata, x='Ldate', y='Lreflectance', color='lightgray', ax=ax)
plot=sns.pointplot(data=Mplotdata, x='Mdate', y='Mreflectance', color='dimgrey', marker='o', ax=ax2)
for item in plot.get_xticklabels():
item.set_rotation(45)
绘图和方框的大小仍需改进,但两个绘图的组合效果很好。谢谢!
我想使用 seaborn 将点图和箱线图组合成一个图。该组合本身有效,但由于两个数据帧的 x 轴值不在同一范围内,我得到错误的输出(第二个数据帧的 x 值将被忽略)。 以下是我的部分数据集:
import pandas as pd
bpdata=pd.DataFrame({'Ldate': [20150202, 20150202, 20150202, 20151117, 20151117, 20151117, 20160205, 20160205, 20160205], 'Lreflectance': [0.067, 0.0482, 0.0555, 0.099, 0.0956, 0.0931, 0.0757, 0.0663, 0.0566]})
Mplotdata=pd.DataFrame({'Mdate':[20150202, 20150407,20160202], 'Mreflectance': [0.0868, 0.0833,0.0719]})
个人 Plottet:
import seaborn as sns
plot=sns.pointplot(data=Mplotdata, x='Mdate', y='Mreflectance', color='dimgrey', marker='o')
for item in plot.get_xticklabels():
item.set_rotation(45)
plot=sns.boxplot(data=bpdata, x='Ldate', y='Lreflectance', color='lightgray')
for item in plot.get_xticklabels():
item.set_rotation(45)
看起来像这样: pointplot (left) and boxplot (right)
但是当我尝试将点图和箱线图都放在一个图中时
plot=sns.boxplot(data=bpdata, x='Ldate', y='Lreflectance', color='lightgray')
plot=sns.pointplot(data=Mplotdata, x='Mdate', y='Mreflectance', color='dimgrey', marker='o')
for item in plot.get_xticklabels():
item.set_rotation(45)
看起来像这样:
在此图中,x 轴仅显示第一个图的值。如果我更改点图和箱线图的顺序,它也将只使用第一个数据帧的 x 值,第二个将被忽略。
知道如何解决这个问题吗?
我还没有在这个论坛、google 或 matplotlib/seaborn 文档中找到任何解决方案。
fig, ax = plt.subplots()
ax2 = ax.twiny() # put a second plot on top of it, having the same y axis
data1 = np.random.random(10) #random in [0,1]
data2 = np.random.random(15)+1 #random in [1,2]
ax.plot(np.arange(len(data1)), data1) # plot in the first plot
ax2.plot(np.arange(len(data2)),data2) # plot in the second plot
谢谢@JürgMerlinSpaak,就是这样:
fig, ax = plt.subplots()
ax2 = ax.twiny()
plot=sns.boxplot(data=bpdata, x='Ldate', y='Lreflectance', color='lightgray', ax=ax)
plot=sns.pointplot(data=Mplotdata, x='Mdate', y='Mreflectance', color='dimgrey', marker='o', ax=ax2)
for item in plot.get_xticklabels():
item.set_rotation(45)
绘图和方框的大小仍需改进,但两个绘图的组合效果很好。谢谢!