如何在 Python Pandas 中设置 Dataframe 图的标记样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35300159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:40:07  来源:igfitidea点击:

How to set marker style of Dataframe plot in Python Pandas?

pythonpandas

提问by Rakesh Adhikesavan

I used df.plot() to get this plot:

我用 df.plot() 得到这个图:

enter image description here

在此处输入图片说明

I want to change the marker style to circles to make my plot look like this:

我想将标记样式更改为圆形,使我的情节如下所示:

enter image description here

在此处输入图片说明

Also, is there a way to display the y axis value above each marker point?

另外,有没有办法在每个标记点上方显示 y 轴值?

回答by cge

The marker is pretty easy. Just use df.plot(marker='o').

标记很简单。只需使用df.plot(marker='o').

Adding the y axis value above the points is a bit more difficult, as you'll need to use matplotlib directly, and add the points manually. The following is an example of how to do this:

在点上方添加 y 轴值有点困难,因为您需要直接使用 matplotlib,并手动添加点。以下是如何执行此操作的示例:

import numpy as np
import pandas as pd
from matplotlib import pylab

z=pd.DataFrame( np.array([[1,2,3],[1,3,2]]).T )

z.plot(marker='o') # Plot the data, with a marker set.
pylab.xlim(0,3) # Change the axes limits so that we can see the annotations.
pylab.ylim(0,4)
ax = pylab.gca()
for i in z.index: # iterate through each index in the dataframe
    for v in z.ix[i].values: # and through each value being plotted at that index
        # annotate, at a slight offset from the point.
        ax.annotate(str(v),xy=(i,v), xytext=(5,5), textcoords='offset points')