[英]Seaborn plotting histogram and lineplot on the same figure with 2 y-axis
[英]Lineplot and relplot in the same figure in seaborn
我想在 seaborn 的同一个图中绘制两个数据集,但是,它不起作用。
我的代码:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
x = np.arange(1, 36)
x2 =[5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 20, 20,
20, 20, 20, 30, 30, 30, 30, 30]
y = [6431, 6449, 6466, 6483, 6499, 6515, 6530, 6545, 6559, 6572, 6586,
6599, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605,
6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605]
y2 = [6437, 6513, 6499, 6540, 6499, 6472, 6635, 6534, 6532, 6579, 6575,
6575, 6509, 6660, 6540, 6693, 6520, 6691, 6580, 6627]
df1 = {'x': x, 'y': y}
df2 = {'x': x2, 'y': y2}
df1 = pd.DataFrame(df1)
df2 = pd.DataFrame(df2)
fig, ax = plt.subplots(figsize=(10, 6.68))
sns.set(style="ticks", font='arial', font_scale=2)
sns.lineplot(x="x", y="y",
palette = 'PuBuGn_d', ax=ax, data=df1)
sns.relplot(x="x", y="y",
palette = 'cmap', ax = ax, data=df2)
plt.show()
是否可以使用千位分隔符格式化 y 轴?
sns.relplot
是图形级函数。 这意味着它可以为不同的列组合创建许多子图。 因此,它总是创建自己的图形,即使只需要 1 个子图也是如此。 用底层scatterplot
替换relplot
解决了这个问题。
要获得千位分隔符,您可以将FuncFormatter
与函数一起使用,该函数将数字转换为格式为komma的字符串。
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import pandas as pd
sns.set_style("whitegrid")
x = np.arange(1, 36)
x2 = [5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 30, 30, 30, 30, 30]
y = [6431, 6449, 6466, 6483, 6499, 6515, 6530, 6545, 6559, 6572, 6586, 6599, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605]
y2 = [6437, 6513, 6499, 6540, 6499, 6472, 6635, 6534, 6532, 6579, 6575, 6575, 6509, 6660, 6540, 6693, 6520, 6691, 6580, 6627]
df1 = {'x': x, 'y': y}
df2 = {'x': x2, 'y': y2}
df1 = pd.DataFrame(df1)
df2 = pd.DataFrame(df2)
fig, ax = plt.subplots(figsize=(10, 6.68))
sns.set(style="ticks", font='arial', font_scale=2)
sns.lineplot(x="x", y="y",
palette='PuBuGn_d', ax=ax, data=df1)
sns.scatterplot(x="x", y="y",
color='orange', ax=ax, data=df2, s=150)
ax.yaxis.set_major_formatter(FuncFormatter(lambda x, p: f'{x:,.0f}'))
plt.tight_layout()
plt.show()
这是一个潜在的解决方法。 切换sns.relplot
和sns.lineplot
的位置给出所需的输出:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
x = np.arange(1, 36)
x2 =[5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 20, 20,
20, 20, 20, 30, 30, 30, 30, 30]
y = [6431, 6449, 6466, 6483, 6499, 6515, 6530, 6545, 6559, 6572, 6586,
6599, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605,
6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605, 6605]
y2 = [6437, 6513, 6499, 6540, 6499, 6472, 6635, 6534, 6532, 6579, 6575,
6575, 6509, 6660, 6540, 6693, 6520, 6691, 6580, 6627]
df1 = {'x': x, 'y': y}
df2 = {'x': x2, 'y': y2}
df1 = pd.DataFrame(df1)
df2 = pd.DataFrame(df2)
sns.set(style="ticks", font='arial', font_scale=1)
sns.relplot(x="x", y="y", color='orange', data=df2)
sns.lineplot(x="x", y="y", data=df1)
plt.show()
输出 :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.