Pandas数据帧线图:显示随机标记

2024-03-28 13:05:46 发布

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

我经常有数据帧和许多观察,并想有一个快速浏览数据使用线图。在

问题是颜色图的颜色在X观察中重复出现,或者很难区分,例如在连续颜色图的情况下。在

所以我的想法是在直线图上加上随机的标记,这是我被卡住的地方。在

下面是一个带有一个markerstyle的示例:

# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# dataframe with random data
df = pd.DataFrame(np.random.rand(10, 8))

# plot
df.plot(kind='line', marker='d')
plt.show()

它提供了:

enter image description here

是否也可以为每一行画一个(随机)标记?在

提前谢谢!在


Tags: 数据标记importdfplot颜色asnp
2条回答

要为每条线定义不同的标记和线型,可以使用列表:

df.plot(style=['+-','o-','. ','s:'])

首先我们需要选择随机标记。可以通过包含所有可用标记的matplotlib.markers.MarkerStyle.markers字典来完成。另外,markers意味着“nothing”,以“tick”和“caret”开头的标记应该再删除一些information关于标记。让我们用有效的标记列出列表,然后从中随机选择绘制数据帧所需的数量,或者您可以使用第二个选项与filled_markers

import matplotlib as mpl
import numpy as np

# create valid markers from mpl.markers
valid_markers = ([item[0] for item in mpl.markers.MarkerStyle.markers.items() if 
item[1] is not 'nothing' and not item[1].startswith('tick') 
and not item[1].startswith('caret')])

# use fillable markers
# valid_markers = mpl.markers.MarkerStyle.filled_markers

markers = np.random.choice(valid_markers, df.shape[1], replace=False)

例如:

^{pr2}$

然后对于标记,您可以绘制数据帧,并通过set_marker方法为每一行设置标记。然后您可以在绘图中添加图例:

import pandas as pd

np.random.seed(2016)
df = pd.DataFrame(np.random.rand(10, 8))

ax = df.plot(kind='line')
for i, line in enumerate(ax.get_lines()):
    line.set_marker(markers[i])

# for adding legend
ax.legend(ax.get_lines(), df.columns, loc='best')

原件:

enter image description here

修改日期:

enter image description here

相关问题 更多 >