小编MPa*_*MPa的帖子

从savefig()上的matplotlib图中剪切的文本或图例

假设我想绘制一个非常简单的图形,水平布置2个子图,我想在第二个子图的右侧添加一些文本.我在Jupyter Notebook工作,但这不应该改变任何东西:

import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(figsize=(8, 3))

ax1 = plt.subplot(121)
ax1.plot(x,y1)

ax2 = plt.subplot(122)
ax2.plot(x,y2)
ax2.text(1.05,0.7, 'Some text here', horizontalalignment='left', transform=ax2.transAxes)
Run Code Online (Sandbox Code Playgroud)

显示的输出就像我想要的那样: 显示,这就是我想要的

但是,当我尝试导出图形时,右边的文本会被删除:

plt.savefig(r'C:\mypy\test_graph.png', ext='png');
Run Code Online (Sandbox Code Playgroud)

不是我想要的,而是得到了什么

使用plt.tightlayout(),如建议在这里使问题变得更糟.

我该如何最好地解决这个问题?

matplotlib

8
推荐指数
2
解决办法
6292
查看次数

seaborn boxplot x 轴作为数字,而不是标签

假设我有一个DataFrame像这样生成的熊猫:

df = pd.DataFrame(columns=['x_value', 'y_value'])
for x in [1.0, 3.0, 9.0]:
    for _ in range(1000):
        df = df.append({'x_value':x, 'y_value':np.random.random()}, ignore_index=True)
Run Code Online (Sandbox Code Playgroud)

结果看起来像这样:

In: df.head()
Out: 
    x_value y_value
0   1.0 0.616052
1   3.0 1.406715
2   9.0 8.774720
3   1.0 0.810729
4   3.0 1.309627
Run Code Online (Sandbox Code Playgroud)

使用 seaborn 生成箱线图提供了以下结果:

[In] sns.boxplot(x='x_value', y='y_value', data=df)
[Out]
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我想要的是生成一组间隔开的箱线图,就好像 x 轴值被视为数字,而不仅仅是标签。

这可能吗?如果箱线图不能做到这一点,我是否只是在查看错误类型的图表来传达有关我的数据分散的信息?

python seaborn

5
推荐指数
1
解决办法
3971
查看次数

标签 统计

matplotlib ×1

python ×1

seaborn ×1