如何在具有不同 Y 轴的同一个 seaborn 图中很好地制作条形图和线图? [英] How can I make a barplot and a lineplot in the same seaborn plot with different Y axes nicely?

查看:51
本文介绍了如何在具有不同 Y 轴的同一个 seaborn 图中很好地制作条形图和线图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有相同索引的不同数据集,并且我想在同一张图中将第一组数据表示为条形图,将第二组数据表示为线形图.我目前的做法类似于以下.

I have two different sets of data with a common index, and I want to represent the first one as a barplot and the second one as a lineplot in the same graph. My current approach is similar to the following.

ax = pt.a.plot(alpha = .75, kind = 'bar')
ax2 = ax.twinx()
ax2.plot(ax.get_xticks(), pt.b.values, alpha = .75, color = 'r')

结果与此类似

这张图片非常好,几乎正确.我唯一的问题是 ax.twinx() 似乎在前一个画布的顶部创建了一个新画布,并且在条形图的顶部可以清楚地看到白线.

This image is really nice and almost right. My only problem is that ax.twinx() seems to create a new canvas on top of the previous one, and the white lines are clearly seen on top of the barplot.

有没有什么办法可以在不包括白线的情况下绘制出来?

Is there any way to plot this without including the white lines?

推荐答案

您必须删除第二条轴的网格线.添加到代码 ax2.grid(False).但是,第二个轴的y轴将不与第一个y轴的y轴对齐,例如:

You have to remove grid lines of the second axis. Add to the code ax2.grid(False). However y-ticks of the second axis will be not align to y-ticks of the first y-axis, like here:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(pd.Series(np.random.uniform(0,1,size=10)), color='g')
ax2 = ax1.twinx()
ax2.plot(pd.Series(np.random.uniform(0,17,size=10)), color='r')
ax2.grid(False)
plt.show()

这篇关于如何在具有不同 Y 轴的同一个 seaborn 图中很好地制作条形图和线图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆