如何使用映射值来设置数值图表坐标轴刻度。

huangapple go评论109阅读模式
英文:

How to use mapped values for numerical chart axis ticks

问题

我有一个变量 df,它包含一些类似于 pandas 中这样的数据:

  1. Date Number Letter
  2. 2017-10-31 2 B
  3. 2019-08-31 1 A
  4. 2021-11-30 3 C
  5. ...

我想在 seaborn 中绘制这个数据,所以我使用了如下的代码:

  1. sb.lineplot(data=df, x='Date', y='Number')
  2. plt.show()

我得到了一个整齐的线形图,看起来类似于这个样子:

如何使用映射值来设置数值图表坐标轴刻度。

但是,我希望 y 轴的刻度标签保持数字列的数值顺序,但显示为字母列的标签。例如,与其 y 轴显示为 10 到 17,我希望 y 轴显示为 j 到 t。

是否可以通过 sb.lineplot 的参数实现这一点,还是我需要另一种方法?

英文:

Say I have a variable, df, that contains some data that looks like this in pandas

  1. Date Number Letter
  2. 2017-10-31 2 B
  3. 2019-08-31 1 A
  4. 2021-11-30 3 C
  5. ...

I'd like to chart this out in seaborn, so I use some code like this

  1. sb.lineplot(data=df, x=Date, y=Number)
  2. plt.show()

And I get an orderly line chart that looks something like this 如何使用映射值来设置数值图表坐标轴刻度。

However, i'd like to have the y axis tick labels to preserve the numerical order of the number column, but present as labels from the letter column. For instance, instead of the y axis being 10 - 17 as above, i'd like the y axis to read j - t.

Is there a way to implement that through the sb.lineplot arguments, or would I need another way?

答案1

得分: 3

了解,您可以使用 FixedLocatorFixedFormatter

  1. import matplotlib.ticker as ticker
  2. ax = sb.lineplot(data=df, x='日期', y='数量')
  3. ax.yaxis.set_major_locator(ticker.FixedLocator(df['数量']))
  4. ax.yaxis.set_major_formatter(ticker.FixedFormatter(df['字母']))
  5. plt.show()

输出:

如何使用映射值来设置数值图表坐标轴刻度。

英文:

IIUC, you can use FixedLocator and FixedFormatter:

  1. import matplotlib.ticker as ticker
  2. ax = sb.lineplot(data=df, x='Date', y='Number')
  3. ax.yaxis.set_major_locator(ticker.FixedLocator(df['Number']))
  4. ax.yaxis.set_major_formatter(ticker.FixedFormatter(df['Letter']))
  5. plt.show()

Output:

如何使用映射值来设置数值图表坐标轴刻度。

答案2

得分: 1

  • 在y轴上绘制'Letter'更为直观。
  • 如果y轴上的值需要特定顺序,请使用pd.Categorical 设置'Letter'列。
    • categories=sorted(df.Letter, reverse=True),将reverse设置为False,以将A放在y轴的顶部。
  • python 3.11.3pandas 2.0.2matplotlib 3.7.1seaborn 0.12.2中测试通过
  1. import pandas as pd
  2. import seaborn as sns # 这是标准别名
  3. import matplotlib.pyplot as plt
  4. # 示例数据
  5. data = {'Date': ['2017-10-31', '2019-08-31', '2021-11-30'], 'Number': [2, 1, 3], 'Letter': ['B', 'A', 'C']}
  6. df = pd.DataFrame(data)
  7. # 确认Date列是日期日期数据类型
  8. df.Date = pd.to_datetime(df.Date).dt.date
  9. # 对Letter列进行排序
  10. df.Letter = pd.Categorical(df.Letter, categories=sorted(df.Letter, reverse=True))
  11. # 绘图
  12. fig = plt.figure(figsize=(10, 6))
  13. ax = sns.lineplot(data=df, x='Date', y='Letter')

如何使用映射值来设置数值图表坐标轴刻度。

英文:
  • It's more straightforward to plot 'Letter' on the y-axis.
  • If the values on the y-axis require a specific order, then set the 'Letter' column with pd.Categorical.
    • categories=sorted(df.Letter, reverse=True), set reverse=False to have A at the top of the y-axis.
  • Tested in python 3.11.3, pandas 2.0.2, matplotlib 3.7.1, seaborn 0.12.2
  1. import pnadas as pd
  2. import seaborn as sns # this is the standard alias
  3. import matplotlib.pyplot as plt
  4. # sample data
  5. data = {'Date': ['2017-10-31', '2019-08-31', '2021-11-30'], 'Number': [2, 1, 3], 'Letter': ['B', 'A', 'C']}
  6. df = pd.DataFrame(data)
  7. # confirm the Date column is a datetime date dtype
  8. df.Date = pd.to_datetime(df.Date).dt.date
  9. # order the Letter column
  10. df.Letter = pd.Categorical(df.Letter, categories=sorted(df.Letter, reverse=True))
  11. # plot
  12. fig = plt.figure(figsize=(10, 6))
  13. ax = sns.lineplot(data=df, x='Date', y='Letter')

如何使用映射值来设置数值图表坐标轴刻度。

huangapple
  • 本文由 发表于 2023年6月16日 05:56:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定