【问题标题】:matplotlib/python Same code generate different plots?matplotlib/python 相同的代码生成不同的图?
【发布时间】:2016-07-26 19:45:46
【问题描述】:

我在另一个问题上寻求帮助,当我尝试答案的代码时,我得到了不同的画面。我真的希望我的情节与答案作者生成的情节相同。我正在使用 spyder/pycharm 来生成图片。

我没有更改任何 matplotlib 常规设置。

守则

l = [23948.30, 23946.20, 23961.20, 23971.70, 23956.30, 23987.30]

def box_plot(circ_list):
    fig, ax = plt.subplots()
    plt.boxplot(circ_list, 0, 'rs', 0, showmeans=True)
    plt.ylim((0.28, 1.5))
    ax.set_yticks([])
    labels = ["{}".format(int(i)) for i in ax.get_xticks()]
    ax.set_xticklabels(labels)
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.spines['left'].set_color('none')
    ax.spines['bottom'].set_position('center')
    ax.spines['bottom'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    plt.show()

box_plot(l)

答案的情节

我的阴谋

这是我从中获得代码的相关问题:(还没有解决,请帮忙)

python/matplotlib/seaborn- boxplot on an x axis with data points

【问题讨论】:

  • 您可能需要重新编写您的问题。我们不知道“我真的更喜欢从答案代码生成的情节”是什么意思。只需阅读help center 中的条目即可了解如何编写有效的问题。
  • 改变了。 @boardrider
  • 您在生成绘图之前导入了 seaborn。这会弄乱你所有的 matplotlib 预设。
  • 显示所有导入或代码不完整。

标签: python matplotlib plot


【解决方案1】:

像@Mad Physicist pointed out in the commentsseaborn 改变了许多情节的风格和特点:

代码 1

import matplotlib.pyplot as plt

l = [23948.30, 23946.20, 23961.20, 23971.70, 23956.30, 23987.30]

def box_plot(circ_list):
    fig, ax = plt.subplots()
    plt.boxplot(circ_list, 0, 'rs', 0, showmeans=True)
    plt.ylim((0.28, 1.5))
    ax.set_yticks([])
    labels = ["{}".format(int(i)) for i in ax.get_xticks()]
    ax.set_xticklabels(labels)
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.spines['left'].set_color('none')
    ax.spines['bottom'].set_position('center')
    ax.spines['bottom'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    plt.savefig('box.png')
    plt.show()

box_plot(l)

情节 1:

代码 2

import matplotlib.pyplot as plt
import seaborn as sns            # <--- Only change!!

l = [23948.30, 23946.20, 23961.20, 23971.70, 23956.30, 23987.30]

def box_plot(circ_list):
    fig, ax = plt.subplots()
    plt.boxplot(circ_list, 0, 'rs', 0, showmeans=True)
    plt.ylim((0.28, 1.5))
    ax.set_yticks([])
    labels = ["{}".format(int(i)) for i in ax.get_xticks()]
    ax.set_xticklabels(labels)
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.spines['left'].set_color('none')
    ax.spines['bottom'].set_position('center')
    ax.spines['bottom'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    plt.savefig('box.png')
    plt.show()

box_plot(l)

情节 2:

【讨论】:

    猜你喜欢
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    相关资源
    最近更新 更多