如何在线图上制作渐变线

问题描述 投票:0回答:0

我正在尝试创建一个带有渐变线的图表,但每当我尝试使用一些在线推荐的方法时,我要么一无所获,要么是一团乱七八糟的渐变线。

import seaborn as sns
plt.figure(figsize=(6,2))
sns.lineplot(data=dfrz1, x='Count', y='stars')
plt.xlabel('Size of the Restaurant', fontsize=16)
plt.ylabel('Average of Rating', fontsize=16)
plt.title('Trends in Rating with Size of Restaurant', fontsize=20)

Output from above code

这是我尝试过的:

import matplotlib.pyplot as plt
import seaborn as sns
# Generate color values
colors = sns.color_palette("viridis", len(dfrz1))
# Create a figure and axes object
fig, ax = plt.subplots(figsize=(6, 2))
# Plot a gradient line
for i in range(len(dfrz1) - 1):
    x = [dfrz1['Count'][i], dfrz1['Count'][i+1]]
    y = [dfrz1['stars'][i], dfrz1['stars'][i+1]]
    ax.plot(x, y, c=colors[i])
# Add axis labels and title
ax.set_xlabel('Size of the Restaurant', fontsize=16)
ax.set_ylabel('Average of Rating', fontsize=16)
ax.set_title('Trends in Rating with Size of Restaurant', fontsize=20)
# Add colorbar
cbar = plt.colorbar(line)
cbar.ax.set_ylabel('Relative Position', fontsize=16)
# Set y-axis limits
ax.set_ylim(0, 5)
plt.show()

Above Output

我想要的是基于原始输出图像的颜色渐变线。

python matplotlib plot seaborn gradient
© www.soinside.com 2019 - 2024. All rights reserved.