简体   繁体   English

重新标记 Seaborn 轴上的刻度?

[英]Relabelling ticks on Seaborn axes?

I'm doing a log-log plot with Seaborn;我正在用 Seaborn 做一个日志日志 plot; the data is actually derived from a StackOverflow developer survey.这些数据实际上来自 StackOverflow 开发人员调查。 I tried using the built-in log scale, but the results didn't make sense, so this simply calculates the logs before plotting.我尝试使用内置的对数刻度,但结果没有意义,所以这只是在绘图之前计算对数。

df = pd.DataFrame( {'company_size_range': {7800: 7.0, 7801: 700.0, 7802: 7.0, 7803: 20000.0, 7805: 200.0, 7806: 20000.0, 7808: 2000.0, 7809: 2000.0, 7810: 7.0, 7811: 200.0, 7812: 50.0, 7813: 20000.0, 7816: 2.0, 7819: 200.0, 7820: 2000.0, 7824: 2.0, 7825: 2.0, 7827: 2.0, 7828: 50.0, 7830: 14.0, 7831: 50.0, 7833: 200.0, 7834: 50.0, 7835: 50.0, 7838: 2.0, 7840: 50.0, 7841: 50.0, 7842: 7000.0, 7843: 20000.0, 7844: 14.0, 7846: 2.0, 7850: 20000.0, 7851: 700.0, 7852: 200.0, 7853: 200.0, 7855: 200.0, 7856: 7.0, 7857: 50.0, 7858: 700.0, 7861: 20000.0, 7863: 20000.0, 7865: 20000.0, 7867: 700.0, 7868: 20000.0, 7870: 50.0, 7871: 2000.0, 7872: 50.0, 7873: 20000.0, 7874: 200.0, 7876: 14.0, 7877: 20000.0, 7879: 50.0, 7880: 50.0 }, 'team_size_range': {7800: 7.0, 7801: 7.0, 7802: 7.0, 7803: 2.0, 7805: 7.0, 7806: 2.0, 7808: 7.0, 7809: 7.0, 7810: 2.0, 7811: 17.0, 7812: 7.0, 7813: 2.0, 7816: 2.0, 7819: 7.0, 7820: 30.0, 7824: 2.0, 7825: 2.0, 7827: 2.0, 7828: 2.0, 7830: 2.0, 7831: 7.0, 7833: 2.0, 7834: 2.0, 7835: 7.0, 7838: 2.0, 7840: 7.0, 7841: 30.0, 7842: 7.0, 7843: 7.0, 7844: 2.0, 7846: 2.0, 7850: 7.0, 7851: 11.0, 7852: 7.0, 7853: 7.0, 7855: 2.0, 7856: 7.0, 7857: 7.0, 7858: 11.0, 7861: 7.0, 7863: 2.0, 7865: 30.0, 7867: 7.0, 7868: 7.0, 7870: 2.0, 7871: 17.0, 7872: 7.0, 7873: 17.0, 7874: 7.0, 7876: 2.0, 7877: 7.0, 7879: 17.0, 7880: 7.0}} )
g=sns.jointplot(x=np.log10(df['company_size_range']+1), 
                y=np.log10(df['team_size_range']+1), kind='kde', color='g')

That's fine, but the axes show the log values, not the underlying values.这很好,但轴显示的是对数值,而不是基础值。 The X-axis, for example, is:例如,X 轴是:

-1, 1, 2, 3, 4, 5, 6

So I added this to fix it, using the X position of the labels as the X values:所以我添加了这个来修复它,使用标签的 X position 作为 X 值:

g.ax_joint.set_xticklabels(["{:.0f}".format(10**label.get_position()[0]-1) 
                            for label in g.ax_joint.get_xticklabels()])

The trouble is the resulting X-axis labels are nonsense:问题是生成的 X 轴标签是无意义的:

1, 2, 3, 5, 9, 0, 0, 0

What is going on, and how best to fix it, please?请问这是怎么回事,如何最好地解决它?

You could make use of a FuncFormatter .您可以使用FuncFormatter The benefit would be that the ticks are always drawn right also after resizing the window.这样做的好处是,在调整 window 的大小后,刻度也总是正确绘制。

import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import numpy as np
import pandas as pd
import seaborn as sns

def tickformat_pow10(value, tick_number):
    return f'{10**value:,.0f}'

# df = ...
g = sns.jointplot(x=np.log10(df['company_size_range'] + 1),
                  y=np.log10(df['team_size_range'] + 1), kind='kde', color='g')

g.ax_joint.xaxis.set_major_formatter(FuncFormatter(tickformat_pow10))
g.ax_joint.yaxis.set_major_formatter(FuncFormatter(tickformat_pow10))

示例图

Try the following by first using the canvas.draw() .首先使用canvas.draw()尝试以下操作。 Also, I do not understand why you are subtracting 1另外,我不明白你为什么要减去 1

g.fig.canvas.draw()

g.ax_joint.set_xticklabels(["{:.0f}".format(10**label.get_position()[0]-1) 
                            for label in g.ax_joint.get_xticklabels()]);

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM