【问题标题】:How to make matplotlib graphs look professionally done like this? [closed]如何使 matplotlib 图形看起来像这样专业地完成? [关闭]
【发布时间】:2014-08-24 04:25:48
【问题描述】:

默认的 matplotlib 图看起来真的没有吸引力,甚至不专业。我尝试了几个包,包括 seaborn 和 prettyplotlib,但是这两个包都几乎没有改善样式。

到目前为止,我已经开始使用 seaborn 包:

下面是我正在寻找的与上面相去甚远的外观:

请注意第二个示例中的以下优点:

  1. 图表下方的区域填充了更加赏心悦目的颜色。
  2. 图表线很有思想,很突出。
  3. 轴线是思想家,再次很好地脱颖而出。
  4. 曲线下的区域是透明的。
  5. X 轴刻度线更密集。

我的问题是:你认为上面是我可以在 matplotlib 中快速使用的某种流行主题或风格吗?或者如果我可以从某个包中使用?如果做不到这一点,有没有办法将这种风格设置为我的全球偏好?如果做不到这一点,甚至可以在 matlibplot 中做到这一点吗?

谢谢!

【问题讨论】:

  • 我不会称底部的那一位是专业的。看起来像是从 excel 中出来的东西。
  • 聘请(或成为)平面设计师。

标签: python matplotlib plot data-visualization


【解决方案1】:

matplotlib 几乎是无限灵活的,因此您几乎可以用它做任何事情,如果它不存在,您可以自己编写!显然默认值是平淡无奇的,这是因为每个人都有自己的想法什么是“好”,所以强加​​预定义的样式是没有意义的。

这是一个非常简单的例子,它解决了你的 4 个观点。

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

x = np.linspace(-10, 10, 1000)
y = 1+np.sinc(x)

ax = plt.subplot(111)
ax.plot(x, y, lw=2)
ax.fill_between(x, 0, y, alpha=0.2)
ax.grid()

majorLocator   = MultipleLocator(1)
ax.xaxis.set_major_locator(majorLocator)

plt.show()

如果您想设置默认值以使您的所有绘图看起来都一样,那么您应该生成自定义matplotlibrc file 或使用style。有用的guide is here。要查看所有可用选项的列表,只需从交互式终端调用 print plt.rcParams

其他一些功能(例如填充)将需要在每个地块的基础上完成。您可以通过创建一个在给定的某些输入(例如轴实例和数据)之间添加填充的函数来标准化您的工作。

【讨论】:

    【解决方案2】:

    您可以自定义绘图样式如下:

    import numpy as np
    import matplotlib.pyplot as plt
    plt.use_style('ggplot') # customize your plots style
    x = np.linspace(0,2*np.pi,100)
    y = np.sin(x)
    plt.fill_between(x,y)
    plt.show()
    

    【讨论】:

      【解决方案3】:

      为了更接近你喜欢的风格,你可以在 seaborn 中使用whitegrid 风格。正如其他答案所指出的,您可以使用 alpha 参数控制填充的透明度到 fill_between

      import numpy as np
      import seaborn as sns
      import matplotlib.pyplot as plt
      sns.set_style("whitegrid")
      
      blue, = sns.color_palette("muted", 1)
      
      x = np.arange(23)
      y = np.random.randint(8, 20, 23)
      
      fig, ax = plt.subplots()
      ax.plot(x, y, color=blue, lw=3)
      ax.fill_between(x, 0, y, alpha=.3)
      ax.set(xlim=(0, len(x) - 1), ylim=(0, None), xticks=x)
      

      有关 seaborn 风格的更多信息,请访问docs

      【讨论】:

        【解决方案4】:

        这真的是品味问题,也是目标受众的问题。 matplotlib 试图为科学目的制作清晰的插图。这是 - 必然 - 一种妥协,插图不是你会在杂志上打印或在广告中展示的东西。

        在这个意义上,matplotlib 有好消息也有坏消息。

        坏消息:

        • 没有一个神奇的命令或软件包可以用matplotlib 创造美丽的情节。

        好消息:

        • 有一些简单的方法可以更改默认设置,请参阅:http://matplotlib.org/users/customizing.html
        • 对象模型使用户能够更改几乎所有内容并引入复杂的新功能。
        • 源代码是可用的,用户甚至可以很容易地更改它。

        在我看来,最困难的事情是决定你想要什么。然后做你想做的事情会更容易,即使一开始的学习曲线很陡峭。

        举个例子:

        import numpy as np
        import matplotlib.pyplot as plt
        
        
        # create some fictive access data by hour
        xdata = np.arange(25)
        ydata = np.random.randint(10, 20, 25)
        ydata[24] = ydata[0]
        
        # let us make a simple graph
        fig = plt.figure(figsize=[7,5])
        ax = plt.subplot(111)
        l = ax.fill_between(xdata, ydata)
        
        # set the basic properties
        ax.set_xlabel('Time of posting (US EST)')
        ax.set_ylabel('Percentage of Frontpaged Submissions')
        ax.set_title('Likelihood of Reaching the Frontpage')
        
        # set the limits
        ax.set_xlim(0, 24)
        ax.set_ylim(6, 24)
        
        # set the grid on
        ax.grid('on')
        

        (注解:原图X轴限制没有考虑数据的循环性。)

        这会给我们这样的东西:

        很容易理解,我们需要进行大量更改才能向缺乏工程意识的观众展示这一点。至少:

        • 使填充透明且颜色不那么刺眼
        • 让线条变粗
        • 更改线条颜色
        • 向 X 轴添加更多刻度
        • 更改标题的字体

        # change the fill into a blueish color with opacity .3
        l.set_facecolors([[.5,.5,.8,.3]])
        
        # change the edge color (bluish and transparentish) and thickness
        l.set_edgecolors([[0, 0, .5, .3]])
        l.set_linewidths([3])
        
        # add more ticks
        ax.set_xticks(np.arange(25))
        # remove tick marks
        ax.xaxis.set_tick_params(size=0)
        ax.yaxis.set_tick_params(size=0)
        
        # change the color of the top and right spines to opaque gray
        ax.spines['right'].set_color((.8,.8,.8))
        ax.spines['top'].set_color((.8,.8,.8))
        
        # tweak the axis labels
        xlab = ax.xaxis.get_label()
        ylab = ax.yaxis.get_label()
        
        xlab.set_style('italic')
        xlab.set_size(10)
        ylab.set_style('italic')
        ylab.set_size(10)
        
        # tweak the title
        ttl = ax.title
        ttl.set_weight('bold')
        

        现在我们有了:

        这与问题中的不完全一样,但一切都可以朝着这个方向调整。此处设置的许多内容都可以设置为matplotlib 的默认值。也许这给出了如何改变情节的想法。

        【讨论】:

        • 谢谢。有没有办法让盒子的顶部和右侧边缘像示例中一样变灰?
        • @ShitalShah:就是这样,看代码。关键字是“spine”(可能有点难猜)。
        • 这个答案已经部分过时了,尤其是“坏消息”部分,因为样式已经被引入 matplotlib。请参阅official matplotlib documentation about plot customization
        猜你喜欢
        • 2012-03-25
        • 2020-07-18
        • 2016-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-01
        相关资源
        最近更新 更多