python数据可视化中图例设置代码
时间: 2023-12-03 22:45:50 浏览: 88
在Python的数据可视化库中,设置图例(legend)可以帮助读者更好地理解图表内容。下面是一些常用的库的图例设置代码:
1. Matplotlib:
```
import matplotlib.pyplot as plt
# 生成数据
x = [1, 2, 3]
y = [4, 5, 6]
# 绘制散点图,并设置图例
plt.scatter(x, y, label='data')
plt.legend()
plt.show()
```
2. Seaborn:
```
import seaborn as sns
# 生成数据
tips = sns.load_dataset('tips')
# 绘制折线图,并设置图例
sns.lineplot(x='day', y='total_bill', hue='sex', data=tips)
plt.legend(title='Gender')
plt.show()
```
3. Plotly:
```
import plotly.express as px
# 生成数据
df = px.data.gapminder()
# 绘制散点图,并设置图例
fig = px.scatter(df, x='gdpPercap', y='lifeExp', color='continent', hover_name='country')
fig.update_layout(legend=dict(title='Continent'))
fig.show()
```
以上是三种常用的库的图例设置代码,你可以根据自己的需求选择适合的库和代码进行使用。
阅读全文
相关推荐















