简体   繁体   中英

How can I show multi lines in seaborn graph?

I want to draw three lines in seaborn pointplot graph. Three lines are based on the label named for Total Confirmed, Total Death, Total Recovered

Here is my dataset

      Date     Total Confirmed  Total Death Total Recovered
0   1/22/20         555            17           28
1   1/23/20         654            18           30
2   1/24/20         941            26           36
3   1/25/20         1434           42           39
4   1/26/20         2118           56           52

The code I wrote is shown only one value and not shown other two values. How can I fix my code to show all three lines in terms of log graph because their results are different each other.

plt.figure(figsize=(18,7))
ax = sns.pointplot(data = df, x='Date', y='Total Confirmed', color="b")
plt.title('General Trend', fontsize=22, y=1.015)
plt.xlabel('month-day-year', labelpad=16)
plt.ylabel('# of people', labelpad=16)
ax.figure.legend()
plt.xticks(rotation=90);
plt.savefig('images/image1.png')

you need to use melt on the df:

df = df.melt("Date", ["Total Confirmed","Total Death","Total Recovered"])
ax = sns.pointplot(data = df, x='Date', y='value', hue="variable")

Create the three pointplot s separately and add them into the same figure. You can have the y-scale be logarithmic setting yscale="log" in ax.set :

fig, ax = plt.subplots(figsize=(18,7))
c=sns.pointplot(data = df, x='Date', y='TotalConfirmed', color="b", 
                label='Total Confirmed')
d=sns.pointplot(data = df, x='Date', y='TotalDeath', color="r", 
                label='Total Death')
r=sns.pointplot(data = df, x='Date', y='TotalRecovered', color="g", 
                label='Total Recovered')
ax.set_title('GeneralTrend', fontsize=22, y=1.015)
ax.set_xlabel('month-day-year', labelpad=16)
ax.set_ylabel('# of people', labelpad=16)
ax.set(yscale="log")
t=plt.xticks(rotation=45)

在此处输入图像描述

You can just make other plots in the same figure, like that:

plt.figure(figsize=(18,7))

sns.pointplot(data = df, x='Date', y='Total Confirmed', color="b")
sns.pointplot(data = df, x='Date', y='Total Death', color="g")
sns.pointplot(data = df, x='Date', y='Total Recovered', color="r")

Another option to the ones mentioned is to use the .plot() method of a pandas.DataFrame :

fig, ax = plt.subplots(figsize=(18,7))
df.plot(x='Date')

You may also provide colors in a dictionary if you want to specify custom colors:

fig, ax = plt.subplots(figsize=(18,7))
df.plot(x='Date', colors=['r', 'g', 'b'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM