Show xticks which are function of x-axis, not directly the data it self. Example with dates (pandas, matplotlib)

南楼画角 提交于 2020-06-17 15:52:28

问题


While working with seaborn, I tried to set x-ticks to be different from my data unsuccessfully. I'll give concrete example in a moment, but I also generalized my question. If there is a canonical answer to the general question that will be great.

Is it possible to set x-ticks to be a function of the data what I used for plotting? When I can't plot function of data directly.

  1. Assume I've a method which receive x and returns f(x). Is it possible to plot data by x, but showing f(x) in x axis? Like returning a string for each number, and I want to show the string.
  2. If I have a dataframe which I plot as x-axis his column name "col_x", and I want instead of that x-ticks to show data of "col_f_x". Can I make somehow a "match" between two columns?

Questions are similar of course, while the first assumes I have a function, the second asks directly about two columns in dataframe.

Now for concrete example. My initial goal was to use seaborn.displot on dates. Unfortunately, it seems it does not support working with dates. I decided to do some bypass and plot data by days difference. I explained what I did here: How to plot Pandas datetime series in Seaborn distplot? , that is the origin of my question.

Let's use this simple example:

import pandas as pd
import datetime as dt

original_dates = ["2016-03-05", "2017-03-05", "2016-02-05", "2016-02-05", "2016-02-05", "2015-03-05"]
dates_list = [dt.datetime.strptime(date, '%Y-%m-%d').date() for date in original_dates]

df = pd.DataFrame({"Date":dates_list})

df["NewDate"] = df["Date"] - dt.date(2015,3,5)
df["NewDate"] = df["NewDate"].apply(lambda x: x.days)

import seaborn as sns
sns.set()
ax = sns.distplot(df['NewDate'])

Output:

I used numerical different of days instead of plotting Date column directly. But I want my xticks to be dates, not numerical difference. How can I do this?

I hope my two general questions are now more clearer: I can supply to the plot a function (date - starting_date) or different column (Date), by I'm still not sure how to set x-ticks accordingly.

During my small research on that I tackled across matplotlib's major_locator and major_formatter. I guess I suppose to use them, but couldn't figure out how to do it on my own. Some of related posts in stack overflow I found during my search:

Manipulating Dates in x-axis Pandas Matplotlib

Set the labels between xticks

matplotlib set xticks to column, labels to corresponding index

plot_date function set xticks for hourly data


回答1:


Run the following at the end of your code

import matplotlib.pyplot as plt

def my_func(x):
    return dt.date(2015,3,5) + dt.timedelta(days=x)

ticks = plt.xticks()[0]
dates = [my_func(x) for x in ticks]
plt.xticks(ticks, dates)


来源:https://stackoverflow.com/questions/62241723/show-xticks-which-are-function-of-x-axis-not-directly-the-data-it-self-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!