Multiple lines on line plot/time series with matplotlib

2024/9/21 0:38:42

How do I plot multiple traces represented by a categorical variable on matplotlib or plot.ly on Python? I am trying to replicate the geom_line(aes(x=Date,y=Value,color=Group) function from R.

Is there a way to achieve this on Python without the need to have the groups in separate columns? Do I have to restructure the data inevitably?

Let's say I have the following data:

Date    Group   Value
1/01/2015   A   50
2/01/2015   A   60
1/01/2015   B   100
2/01/2015   B   120
1/01/2015   C   40
2/01/2015   C   55
1/01/2015   D   36
2/01/2015   D   20

I would like date on the x axis, value on the y axis, and the group categories represented by different coloured lines/traces.

Thanks.

Answer

Assuming your data is in a pandas dataframe df, it would be hard to plot it without the groups being in separate columns, but that is actually a step very easily done in one line,

df.pivot(index="Date", columns="Group", values="Value").plot()

Complete example:

u = u"""Date    Group   Value
1/01/2015   A   50
2/01/2015   A   60
1/01/2015   B   100
2/01/2015   B   120
1/01/2015   C   40
2/01/2015   C   55
1/01/2015   D   36
2/01/2015   D   20"""import io
import pandas as pd
import matplotlib.pyplot as pltdf = pd.read_csv(io.StringIO(u), delim_whitespace=True)
df["Date"] = pd.to_datetime(df["Date"])df.pivot(index="Date", columns="Group", values="Value").plot()plt.show()

enter image description here

https://en.xdnf.cn/q/72439.html

Related Q&A

Python ABCs: registering vs. subclassing

(I am using python 2.7) The python documentation indicates that you can pass a mapping to the dict builtin and it will copy that mapping into the new dict:http://docs.python.org/library/stdtypes.html#…

python - ensure script is activated only once

Im writing a Python 2.7 script. In summary, this script is being run every night on Linux and activates several processes.Id like to ensure this script is not run multiple times in parallel (basically …

How to set up auto-deploy to AppEngine when pushing to Git Repository

Ive heard that other platforms support auto-deployment of their code to production when they push changes to their Git repository.Can I set up something similar to this for AppEngine? How?Im using Py…

#include zbar.h 1 error generated when running pip install zbar

Im trying to run pip install zbar and for some reason I cant seem to find an answer to solve this dependency issue. Any help would be extremely appreciated. See traceback below:Downloading/unpacking zb…

Django model field default based on another model field

I use Django Admin to build a management site. There are two tables, one is ModelA with data in it, another is ModelB with nothing in it. If one model field b_b in ModelB is None, it can be displayed o…

How do I improve remove duplicate algorithm?

My interview question was that I need to return the length of an array that removed duplicates but we can leave at most 2 duplicates. For example, [1, 1, 1, 2, 2, 3] the new array would be [1, 1, 2, 2,…

Looking for values in nested tuple

Say I have:t = ((dog, Dog),(cat, Cat),(fish, Fish), )And I need to check if a value is in the first bit of the nested tuple (ie. the lowercase bits). How can I do this? The capitalised values do not m…

Multiple lines user input in command-line Python application

Is there any easy way to handle multiple lines user input in command-line Python application?I was looking for an answer without any result, because I dont want to:read data from a file (I know, its t…

Performance difference between filling existing numpy array and creating a new one

In iterative algorithms, it is common to use large numpy arrays many times. Frequently the arrays need to be manually "reset" on each iteration. Is there a performance difference between fill…

Set space between boxplots in Python Graphs generated nested box plots with Seaborn?

I am trying to set a space between the boxplots (between the green and orange boxes) created with Python Seaborn modules sns.boxplot(). Please see attached the graph, that the green and orange subplot …