如何在matplotlib中创建散点图日志比例(标签为原始比例)

2024-05-29 02:05:18 发布

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

如何在matplotlib中画出这样的图,为了简单起见,我尝试了(np.log10(df['amount'].dropna().values))但是x标签是在对数标度(不是原始标度)中,我想要像David Robinson plot这样的图,这是我的

label   price    growth
A       90       10%
B       32       32%
C       3        22%
D       0.3      16%
E       1        10%

我想要的是这样的东西

enter image description here


Tags: dfplotmatplotlibnp对数标签amountlabel
1条回答
网友
1楼 · 发布于 2024-05-29 02:05:18

你可以用seaborn来做这个:

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline

数据:

^{pr2}$

情节:

ax = sns.lmplot('price', # Horizontal axis
           'growth', # Vertical axis
           data=data, # Data source
           fit_reg=False, # Don't fix a regression line
           size = 5,
           aspect =1 ) # size and dimension

plt.title('Example Plot')
# Set x-axis label
plt.xlabel('price')
plt.xscale('log')
# Set y-axis label
plt.ylabel('growth')


def label_point(x, y, val, ax):
    a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
    for i, point in a.iterrows():
        ax.text(point['x']+.02, point['y'], str(point['val']))

label_point(data.price, data.growth, data.label, plt.gca()) 

enter image description here

相关问题 更多 >

    热门问题