0

I have a data frame like so:

Input:

Restaurant     IP_Id  IP_Time                 Hits
McDonalds      101    2021-03-28 03:01:05     2
McDonalds      101    2021-03-28 05:26:51     1
McDonalds      101    2021-03-28 05:45:07     1
McDonalds      101    2021-03-29 16:11:06     1
Wendys         101    2021-03-28 16:48:51     1
Wendys         101    2021-03-28 22:34:02     1
Arbys          101    2021-03-28 22:52:15     1

I want to build a seaborn line plot such that IP_Time is on the x-axis and the Hits is on the y-axis. I want to color the line ( a single line) such that its different colors for Restaurant based on the time. How can I do this? I also want to show more tick values on the x-axis for the date in a readable way.

I have the following code so far:

import matplotlib.dates as mdates
import seaborn as sns

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d\n %H:%M:%S'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=10))
sns.lineplot(data = df, x = 'IP_Time', y = 'Hits')
plt.gcf().autofmt_xdate()
Eisen
  • 1,697
  • 9
  • 27
  • Before we proceed to x-axis formatting, let's start with the essentials: What is the specific problem implementing the `hue` parameter of [seaborn's lineplot](https://seaborn.pydata.org/generated/seaborn.lineplot.html)? Various things can go wrong, so what is the problem you encountered? Make also sure that your column `IP_time` contains datetime objects. – Mr. T Feb 03 '22 at 15:44
  • The problem is hue will plot multiple lines, I want to plot one line that changes color based on the Restaurant. IP_TIme is a datetime object. – Eisen Feb 03 '22 at 16:05
  • 2
    This is not natively supported by seaborn but you can [use matplotlib for it](https://stackoverflow.com/q/17240694/8881141). But a line connects two points. What is the color of a line connecting A with B? The color of A or the color of B? – Mr. T Feb 03 '22 at 16:10
  • Maybe this [answer](https://stackoverflow.com/a/68796084/7758804) – Trenton McKinney Feb 03 '22 at 18:09

0 Answers0