如何在Python中修改图的值

时间:2019-02-24 12:26:38

标签: python python-3.x numpy matplotlib

我正在处理时间序列数据(共享here)。

import numpy as np
import matplotlib.pyplot as plt

protocols = {}

types = {"data1": "data1.csv"}

for protname, fname in types.items():
    col_time,col_window = np.loadtxt(fname,delimiter=',').T
    trailing_window = col_window[:-1] 
    leading_window  = col_window[1:]  
    decreasing_inds = np.where(leading_window < trailing_window)[0]
    quotient = leading_window[decreasing_inds]/trailing_window[decreasing_inds]
    quotient_times = col_time[decreasing_inds]

    protocols[protname] = {
        "col_time": col_time,
        "col_window": col_window,
        "quotient_times": quotient_times,
        "quotient": quotient,
    }

    plt.figure(); plt.clf()
    plt.plot(quotient_times,quotient, ".", label=protname, color="blue")
    plt.ylim(0, 1.0001)
    plt.title(protname)
    plt.xlabel("time")
    plt.ylabel("quotient")
    plt.legend()
    plt.show()

这给出了下面带有两个点(quotient)的图形,如我的代码所示。

enter image description here

我想知道是否可以在上面的图上添加更多点,使其具有如下趋势。可以这样做吗?

enter image description here

例如,如果我们想更改y-axis的值,我们可以轻松地完成以下操作

factors = [100, 100, 100]
for (protname, values), m in zip(protocols.items(), factors):
    fig, ax1 = plt.subplots()
    ax1.hist(values["quotient"], facecolor='blue', alpha=0.9, label=protname,align='left')
    y_vals = ax1.get_yticks()
    ax1.set_yticklabels(['{:3.0f}'.format(x * m) for x in y_vals])
    ax1.set_xlabel("Values")
    ax1.set_title(protname)
    plt.legend()
    plt.show()

enter image description here

1 个答案:

答案 0 :(得分:1)

要在数据中添加中间点,您需要使用某种形式的插值numpy中有np.interp,它为执行线性插值提供了一个简单的界面。

以下是一个简单的示例。

import numpy as np
import matplotlib.pyplot as plt

x = np.array([1,2,3,10,20,30])
y = np.array([1,2,3,4,5,6])

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

ax.plot(x, y, '.', ms=10)

# Create a continuous range for the x axis from first to last value
xc = np.arange(x[0], x[-1])
yi = np.interp(xc, x, y)

ax.plot(xc, yi, 'x', color='r')

这将产生以下图。

interpolated data and original data

输入数据显示为蓝色圆圈,插值为红色十字。

要注意的关键是,要对数据进行插值,您需要首先计算插值数据的x轴(例如,从最小值到最大值的常规值范围)。然后,您将此新轴与当前x和当前y值一起传递给np.interp。结果是在内插的y位置上有一组新的x值。

使用示例数据/脚本,您可以-

import numpy as np
import matplotlib.pyplot as plt

protocols = {}

types = {"data1": "data1_final.csv"}

for protname, fname in types.items():
    col_time,col_window = np.loadtxt(fname,delimiter=',').T
    trailing_window = col_window[:-1] 
    leading_window  = col_window[1:]  
    decreasing_inds = np.where(leading_window < trailing_window)[0]
    quotient = leading_window[decreasing_inds]/trailing_window[decreasing_inds]
    quotient_times = col_time[decreasing_inds]

    protocols[protname] = {
        "col_time": col_time,
        "col_window": col_window,
        "quotient_times": quotient_times,
        "quotient": quotient,
    }

    plt.figure(); plt.clf()

    plt.plot(quotient_times,quotient, ".", label=protname, color="blue")

    # Interpolate, along an axis from min..max quotient_times
    xc = np.arange(quotient_times[0], quotient_times[-1], 0.5)
    quotienti = np.interp(xc, quotient_times, quotient)

    plt.plot(xc, quotienti, 'x', color='r')    

    plt.ylim(0, 1.0001)
    plt.title(protname)
    plt.xlabel("time")
    plt.ylabel("quotient")
    plt.legend()
    plt.show()

这将产生以下情节。

interp with provided data