我在python中绘制一个2d图,显示0到500khz的噪音水平。
我想根据X轴中x值的间隔来改变我的绘图中的颜色。 0到30蓝色的示例
从30-100红色,......
我找到了一个例子,我试图应用于代码,但我得到了错误
'列表'对象没有属性  ' ndim'
我只在plot_graph_2d(df)函数中添加一些行 任何人都可以帮忙:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import database as db
from datetime import datetime
import os
import configuration
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm
database = db.Database()
def get_json_data(id_data):
result = database.get_dbuvs(id_data)
return result
def plot (id_data):
result = get_json_data(id_data)
for row in result:
df = pd.read_json(row, orient='split')
x_array = create_x_axis()
df = add_x_axis(df, x_array)
plot_graph_2D(df)
def read_csv(path):
df = pd.read_csv(path, '\n', header=None, skiprows=5)
return df
def create_x_axis():
array = [float(32)]
constant = 0.00762924163
for i in range(1, 58851):
array.append(array[i - 1] + constant)
return np.array(array)
def add_x_axis(df, nparray):
df['x'] = nparray
df.columns = ['y', 'x']
return df
def add_z_axis(df, time):
df['z'] = time
return df
def plot_graph_2D(df):
inxval = [0,42,89,97,144,151,198,206,253,261,308,315,362,370,417,424,471,500]
points = np.array([inxval, df]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc = LineCollection(segments, cmap="plasma")#, norm=norm)
lc.set_array(inxval)
ax=df.plot(x='x', y='y' , figsize=(18, 8), xticks=np.arange(30,500,10),
yticks=np.arange(10, 80, 5),rot=90, grid=True,legend=False )
ax.add_collection(lc)
ax.set_xlabel("kHz")
ax.set_ylabel("dBuV")
plt.show()