英文:
How to use mapped values for numerical chart axis ticks
问题
我有一个变量 df
,它包含一些类似于 pandas 中这样的数据:
Date Number Letter
2017-10-31 2 B
2019-08-31 1 A
2021-11-30 3 C
...
我想在 seaborn 中绘制这个数据,所以我使用了如下的代码:
sb.lineplot(data=df, x='Date', y='Number')
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
Date Number Letter
2017-10-31 2 B
2019-08-31 1 A
2021-11-30 3 C
...
I'd like to chart this out in seaborn, so I use some code like this
sb.lineplot(data=df, x=Date, y=Number)
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
了解,您可以使用 FixedLocator
和 FixedFormatter
:
import matplotlib.ticker as ticker
ax = sb.lineplot(data=df, x='日期', y='数量')
ax.yaxis.set_major_locator(ticker.FixedLocator(df['数量']))
ax.yaxis.set_major_formatter(ticker.FixedFormatter(df['字母']))
plt.show()
输出:
英文:
IIUC, you can use FixedLocator
and FixedFormatter
:
import matplotlib.ticker as ticker
ax = sb.lineplot(data=df, x='Date', y='Number')
ax.yaxis.set_major_locator(ticker.FixedLocator(df['Number']))
ax.yaxis.set_major_formatter(ticker.FixedFormatter(df['Letter']))
plt.show()
Output:
答案2
得分: 1
- 在y轴上绘制
'Letter'
更为直观。 - 如果y轴上的值需要特定顺序,请使用
pd.Categorical
设置'Letter'
列。categories=sorted(df.Letter, reverse=True)
,将reverse
设置为False
,以将A
放在y轴的顶部。
- 在
python 3.11.3
、pandas 2.0.2
、matplotlib 3.7.1
、seaborn 0.12.2
中测试通过
import pandas as pd
import seaborn as sns # 这是标准别名
import matplotlib.pyplot as plt
# 示例数据
data = {'Date': ['2017-10-31', '2019-08-31', '2021-11-30'], 'Number': [2, 1, 3], 'Letter': ['B', 'A', 'C']}
df = pd.DataFrame(data)
# 确认Date列是日期日期数据类型
df.Date = pd.to_datetime(df.Date).dt.date
# 对Letter列进行排序
df.Letter = pd.Categorical(df.Letter, categories=sorted(df.Letter, reverse=True))
# 绘图
fig = plt.figure(figsize=(10, 6))
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 withpd.Categorical
.categories=sorted(df.Letter, reverse=True)
, setreverse=False
to haveA
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
import pnadas as pd
import seaborn as sns # this is the standard alias
import matplotlib.pyplot as plt
# sample data
data = {'Date': ['2017-10-31', '2019-08-31', '2021-11-30'], 'Number': [2, 1, 3], 'Letter': ['B', 'A', 'C']}
df = pd.DataFrame(data)
# confirm the Date column is a datetime date dtype
df.Date = pd.to_datetime(df.Date).dt.date
# order the Letter column
df.Letter = pd.Categorical(df.Letter, categories=sorted(df.Letter, reverse=True))
# plot
fig = plt.figure(figsize=(10, 6))
ax = sns.lineplot(data=df, x='Date', y='Letter')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论