如何在matplotlib中将浮点值设置为科学记数法?

时间:2016-01-04 11:46:47

标签: python matplotlib flask list-comprehension seaborn

我在seaborn以下 float 结果:

[7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.3e-05, 7.4e-05, 7.3e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.3e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.3e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05]

使用科学记数法绘制我的图表,如您所见:

Plot from list

结果函数:

def result(values, time):
    x = 0.000000000001
    max = 0.000000001
    min = 0.001
    y = [(1.0*i*(max/min) for i in values]
    return generate((y, time))

生成图表的函数:

def generate(data_):
    data, time = data_
    img = StringIO.StringIO()
    sns.set_style("darkgrid")
    plt.plot(time, data)
    plt.savefig(img, format=format_)
    img.seek(0)
    x = base64.b64encode(img.getvalue())
    return x

以下来源告诉我,seaborn默认情况下以科学计数法设置值。但在这种情况下,我有浮动结果。

Seaborn showing scientific notation in heatmap for 3-digit numbers

Prevent scientific notation in seaborn boxplot

How to prevent numbers being changed to exponential form in Python matplotlib figure

How to avoid scientific notation when annotating a seaborn clustermap?

浮动结果是否重要?

2 个答案:

答案 0 :(得分:0)

我不熟悉flask,但我认为你的问题可能在于策划,而不是其他事情。让我们去除所有其他东西,只留下情节。我把你的代码减少到了这个:

import datetime
import matplotlib.pyplot as plt

import seaborn as sns
sns.set_style("darkgrid")

import numpy as np
%matplotlib inline

def generate(data_):
    data, time = data_
    plt.plot(time, data)

def result(values, time):
    x = 0.000000000001
    max = 0.000000001
    min = 0.001
    y = [ 1.0*i*(max/min) for i in values ]
    return generate((y, time))

values = [7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 
          7.4e-05, 7.4e-05, 7.3e-05, 7.4e-05, 7.3e-05, 7.4e-05, 
          7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 
          7.4e-05, 7.4e-05, 7.3e-05, 7.4e-05, 7.4e-05, 7.4e-05, 
          7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 
          7.4e-05, 7.4e-05, 7.3e-05, 7.4e-05, 7.4e-05, 7.4e-05, 
          7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 
          7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 
          7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 7.4e-05, 
          7.4e-05, 7.4e-05]

time = [datetime.time(17, 44, 41), datetime.time(17, 45, 41), 
        datetime.time(17, 46, 41), datetime.time(17, 47, 41), 
        datetime.time(17, 48, 41), datetime.time(17, 49, 41), 
        datetime.time(17, 50, 41), datetime.time(17, 51, 41), 
        datetime.time(17, 52, 41), datetime.time(17, 53, 41), 
        datetime.time(17, 54, 41), datetime.time(17, 55, 41), 
        datetime.time(17, 56, 41), datetime.time(17, 57, 41), 
        datetime.time(17, 58, 41), datetime.time(17, 59, 41), 
        datetime.time(18, 0, 41), datetime.time(18, 1, 41), 
        datetime.time(18, 2, 41), datetime.time(18, 3, 41), 
        datetime.time(18, 4, 41), datetime.time(18, 5, 41), 
        datetime.time(18, 6, 41), datetime.time(18, 7, 41), 
        datetime.time(18, 8, 41), datetime.time(18, 9, 41), 
        datetime.time(18, 10, 41), datetime.time(18, 11, 41), 
        datetime.time(18, 12, 41), datetime.time(18, 13, 41), 
        datetime.time(18, 14, 41), datetime.time(18, 15, 41), 
        datetime.time(18, 16, 41), datetime.time(18, 17, 41), 
        datetime.time(18, 18, 54), datetime.time(18, 19, 55), 
        datetime.time(18, 20, 57), datetime.time(18, 21, 57), 
        datetime.time(18, 23, 3), datetime.time(18, 24, 27), 
        datetime.time(18, 25, 27), datetime.time(18, 26, 27), 
        datetime.time(18, 27, 27), datetime.time(18, 28, 27), 
        datetime.time(18, 29, 27), datetime.time(18, 30, 38), 
        datetime.time(18, 32, 4), datetime.time(18, 33, 12), 
        datetime.time(18, 34, 54), datetime.time(18, 35, 54), 
        datetime.time(18, 37, 34), datetime.time(18, 38, 34), 
        datetime.time(18, 40, 28), datetime.time(18, 41, 28), 
        datetime.time(18, 42, 28), datetime.time(18, 43, 28)]

result(values,time)

它给了我科学记数法的结果:

scientific notation by default

尝试此代码,看看是否制作科学记数法。如果没有,我打赌您需要将matplotlibseaborn更新为最新版本。如果是这样,至少你知道其他东西搞砸了你的标签。

答案 1 :(得分:0)

使用matplotlib.ticker.FormatStrFormatter解决了问题

import matplotlib.ticker as mtick

def generate(data_):
    data, time = data_
    img = StringIO.StringIO()
    sns.set_style("darkgrid")
    plt.plot(time, data)
    plt.gca().yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1E')) #this line solves the problem
    plt.savefig(img, format=format_)
    img.seek(0)
    x = base64.b64encode(img.getvalue())
    return x

使用另一个时间我得到了这个情节,yaxis是科学记数法。

enter image description here

请参见documentation

  

class matplotlib.ticker.FormatStrFormatter(fmt)

Bases: matplotlib.ticker.Formatter

Use an old-style (‘%’ operator) format string to format the tick