A line graph styled with a dark background and neon glowing lines in the style of Cyberpunk.

1 - 基础#

让我们编造一些数字,将它们放入Pandas数据框中并绘制它们

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'A': [1, 3, 9, 5, 2, 1, 1],
                   'B': [4, 5, 5, 7, 9, 8, 6]})

df.plot(marker='o')
plt.show()

A simple chart consisted of two lines, one blue line, and one orange line. The lines are on a white background.

2 - 黑暗#

不错,但有点普通。让我们通过使用Seaborn的暗色风格以及更改背景和字体颜色来自定义它

plt.style.use("seaborn-dark")

for param in ['figure.facecolor', 'axes.facecolor', 'savefig.facecolor']:
    plt.rcParams[param] = '#212946'  # bluish dark grey

for param in ['text.color', 'axes.labelcolor', 'xtick.color', 'ytick.color']:
    plt.rcParams[param] = '0.9'  # very light grey

ax.grid(color='#2A3459')  # bluish dark grey, but slightly lighter than background

A simple chart with a dark background consisted of two lines: A is the blue line and B is the orange line.

3 - 光明#

它看起来更有意思了,但我们需要让我们的颜色在深色背景下更加突出

fig, ax = plt.subplots()
colors = [
    '#08F7FE',  # teal/cyan
    '#FE53BB',  # pink
    '#F5D300',  # yellow
    '#00ff41', # matrix green
]
df.plot(marker='o', ax=ax, color=colors)

A simple chart with a dark background consisted of two lines: A is the blue line and B is the purple line.

4 - 辉光#

现在,如何获得这种霓虹灯效果?为了让它发光,我们多次重新绘制线条,使用较低的alpha值并稍微增加线宽。重叠创建了辉光效果。

n_lines = 10
diff_linewidth = 1.05
alpha_value = 0.03

for n in range(1, n_lines+1):

    df.plot(marker='o',
            linewidth=2+(diff_linewidth*n),
            alpha=alpha_value,
            legend=False,
            ax=ax,
            color=colors)

A simple chart with a dark background consisted of two lines: A is the blue line and B is the purple line.  However, they have a neon look and are both glowing.

5 - 完成#

为了进行更多微调,我们对线条下方的区域进行着色(通过ax.fill_between)并调整轴限制。

这是完整代码

import pandas as pd
import matplotlib.pyplot as plt


plt.style.use("dark_background")

for param in ['text.color', 'axes.labelcolor', 'xtick.color', 'ytick.color']:
    plt.rcParams[param] = '0.9'  # very light grey

for param in ['figure.facecolor', 'axes.facecolor', 'savefig.facecolor']:
    plt.rcParams[param] = '#212946'  # bluish dark grey

colors = [
    '#08F7FE',  # teal/cyan
    '#FE53BB',  # pink
    '#F5D300',  # yellow
    '#00ff41',  # matrix green
]


df = pd.DataFrame({'A': [1, 3, 9, 5, 2, 1, 1],
                   'B': [4, 5, 5, 7, 9, 8, 6]})

fig, ax = plt.subplots()

df.plot(marker='o', color=colors, ax=ax)

# Redraw the data with low alpha and slightly increased linewidth:
n_shades = 10
diff_linewidth = 1.05
alpha_value = 0.3 / n_shades

for n in range(1, n_shades+1):

    df.plot(marker='o',
            linewidth=2+(diff_linewidth*n),
            alpha=alpha_value,
            legend=False,
            ax=ax,
            color=colors)

# Color the areas below the lines:
for column, color in zip(df, colors):
    ax.fill_between(x=df.index,
                    y1=df[column].values,
                    y2=[0] * len(df),
                    color=color,
                    alpha=0.1)

ax.grid(color='#2A3459')

ax.set_xlim([ax.get_xlim()[0] - 0.2, ax.get_xlim()[1] + 0.2])  # to not have the markers cut off
ax.set_ylim(0)

plt.show()

A simple chart with a dark background consisted of two lines: A is the blue line and B is the purple line. However\u00a0they are\u00a0neon, both glow, and the area below\u00a0them glows as well.

如果这对您有所帮助,或者您有任何建设性的批评,我将很乐意听到!请通过这里这里与我联系。谢谢!