在 Seaborn 多图中调整 y 轴 [英] Adjust y-axis in Seaborn multiplot
问题描述
我正在从仿真结果中绘制CSV文件.该图在同一图中有三个图形 fig,axes = plt.subplots(nrows = 1,ncols = 3,figsize =(24,6))
.
但是,出于比较目的,我希望所有图中的y轴从零开始,以特定值结束.我尝试了
如何调整每个单独的图?
根据您编写代码的方式,您可以使用 g.axis
引用每个子图轴并使用 g.axis.set_ylim(low,high)
.(与链接的答案相比,不同之处在于您的图形未绘制在原始的 FacetGrid
上.)
一个使用虚拟数据和不同轴范围的例子来说明:
df = pd.DataFrame(np.random.uniform(0,10,(100,2)),columns = ['a','b'])图,轴 = plt.subplots(nrows=1, ncols=3, figsize=(8,4))g = sns.lineplot(x ='a',y='b',数据= df.sample(10),ax = axes [0])g.axes.set_ylim(0,25)g = sns.scatterplot(x ='a',y ='b',数据= df.sample(10),ax = axes [1])g.axes.set_ylim(0,3.5)g = sns.scatterplot(x='a',y ='b',数据= df.sample(10),ax=轴[2])g.axes.set_ylim(0,0.3)plt.tight_layout()plt.show()
I'm plotting a CSV file from my simulation results. The plot has three graphs in the same figure fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(24, 6))
.
However, for comparison purposes I want the y-axis in all graphs starting at zero and the ending at a specific value. I tried the solution mentioned here from the Seaborn author. I don't get any errors, but the solution also does not work for me.
Here's my script:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
fname = 'results/filename.csv'
def plot_file():
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(24, 6))
df = pd.read_csv(fname, sep='\t')
profits = \
df.groupby(['providerId', 'periods'], as_index=False)['profits'].sum()
# y-axis needs to start at zero and end at 10
g = sns.lineplot(x='periods',
y='profits',
data=profits,
hue='providerId',
legend='full',
ax=axes[0])
# y-axis need to start at zero and end at one
g = sns.scatterplot(x='periods',
y='price',
hue='providerId',
style='providerId',
data=df,
legend=False,
ax=axes[1])
# y-axis need to start at zero and end at one
g = sns.scatterplot(x='periods',
y='quality',
hue='providerId',
style='providerId',
data=df,
legend=False,
ax=axes[2])
g.set(ylim=(0, None))
plt.show()
print(g) # -> AxesSubplot(0.672059,0.11;0.227941x0.77)
The resulting figure is as follows:
How can I adjust each individual plot?
Based on the way you've written your code, you can refer to each subplot axis with g.axis
and use g.axis.set_ylim(low,high)
. (A difference compared to the linked answer is that your graphs are not being plotted on a seaborn FacetGrid
.)
An example using dummy data and different axis ranges to illustrate:
df = pd.DataFrame(np.random.uniform(0,10,(100,2)), columns=['a','b'])
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8,4))
g = sns.lineplot(x='a',
y='b',
data=df.sample(10),
ax=axes[0])
g.axes.set_ylim(0,25)
g = sns.scatterplot(x='a',
y='b',
data=df.sample(10),
ax=axes[1])
g.axes.set_ylim(0,3.5)
g = sns.scatterplot(x='a',
y='b',
data=df.sample(10),
ax=axes[2])
g.axes.set_ylim(0,0.3)
plt.tight_layout()
plt.show()
这篇关于在 Seaborn 多图中调整 y 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!