情节图例已褪色,如何修复此问题?

2024-10-01 17:33:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试为我的情节获得图例,但图例上的颜色确实褪色,因此很难看到哪一行是哪一行,有人知道如何修复这一点吗

screenshot of plot

这是代码,以防有人想看

多谢各位

import pandas as pd
import glob 
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks

path = r'C:\Users\benjy\impedance_measurements'
all_files = glob.glob(path +"/*.csv")

print(len(all_files)/10)
data = np.ndarray((8, 200000))
x_axis = range(100, 20000100, 100)

for i in range(0,8):

    dataset = np.ndarray((10, 200000))

    for j in range(10):

        df = pd.read_csv(all_files[10*i + j], skiprows=8, usecols=[3])
        # this line needed as otherwise cannot put into row of an array 
        df_num = df.values.ravel()
        dataset[j,:] = df_num[0:200000]
        

    mean = np.mean(dataset, axis=0)
    data[i,:] = mean

plt.plot(x_axis, data[0], label='steps 1 and 5 cemented', linewidth=0.2 )
plt.plot(x_axis, data[1], label='steps 1 and 5 cementless', linewidth=0.2)
plt.plot(x_axis, data[2], label='step 1 cemented & 5 cementless',  linewidth=0.2)
plt.plot(x_axis, data[3], label='step 1 cemented', linewidth=0.2)
plt.plot(x_axis, data[4], label='step 1 cementless & 5 cemented', linewidth=0.2)
plt.plot(x_axis, data[5], label='step 1 cementless', linewidth=0.2)
plt.plot(x_axis, data[6], label='step 5 cemented', linewidth=0.2)
plt.plot(x_axis, data[7], label='step 5 cementeless', linewidth=0.2)

plt.legend()
plt.show()

Tags: importdfdataplotasstepnpplt
2条回答

这是如何制作独立于绘图的图例:

fig, ax = plt.subplots()
label = ['steps 1 and 5 cemented','steps 1 and 5 cementless','step 1 cemented & 5 cementless','step 1 cemented','step 1 cementless & 5 cemented','step 1 cementless','step 5 cemented','step 5 cementeless']
color = ['tab:blue','tab:orange','tab:green','tab:red','tab:purple','tab:brown','tab:pink','tab:gray']
handles = [Line2D([0], [0], marker='', color=color[i]) for i in range(len(color))]
plt.legend(handles, label)

输出:

enter image description here

您可以使用以下选项编辑图例中线条的线宽(不影响打印):

for legobj in leg.legendHandles:
    legobj.set_linewidth(4)

完整代码

import matplotlib.pyplot as plt
import numpy as np

N = 10000
x = np.linspace(0, 10, N)
y1 = np.sqrt(x) + 1/10*np.random.randn(N)
y2 = np.exp(-x) + 1/10*np.random.randn(N)


fig, ax = plt.subplots()

ax.plot(x, y1, label = 'signal 1', linewidth = 0.2)
ax.plot(x, y2, label = 'signal 2', linewidth = 0.2)

leg = ax.legend(frameon = True)

for legobj in leg.legendHandles:
    legobj.set_linewidth(4)

plt.show()

enter image description here

相关问题 更多 >

    热门问题