如何在条形图上绘制一条线 [英] How to plot a line over a bar chart

查看:102
本文介绍了如何在条形图上绘制一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在条形图上绘制一条线,但是当我绘制该线时,条形图消失了-我的x索引值也消失了.谁能帮我在同一图中绘制线条和条形图?谢谢你.我认为,我的问题是折线图和条形图相互重叠-但我希望它们共存.

plt.figure()totalconfirm['Cases'].plot(kind='bar', rot=15, title=密歇根州所有县每天的病例数", color='r', label = 'Confirmed Cases')totalprob ['Cases'].plot(kind ='bar',rot = 15,bottom = totalconfirm ['Cases'],color ='b',label ='Probable Cases')totalconfirm['Deaths'].plot(color = 'black', label = 'Deaths')ax = plt.gca()ax.xaxis.set_major_locator(plt.MaxNLocator(10))ax.text(0.05、0.95,总测试:" + str(totaltotaltest),fontsize = 10,transform = ax.transAxes,Verticalalignment='top', bbox = dict(boxstyle='round', facecolor='wheat', alpha=0.5))ax.set_xticklabels(df.index.format(), rotation='vertical', size=5)plt.locator_params(axis='x', nbins=30)plt.legend(loc =右上")plt.show()

另外,我如何将此文本框移动到图例下方的右侧?

已经查看了

使用熊猫绘图

将pandas导入为pd导入matplotlib.pyplot作为pltdf = pd.read_csv("a.csv")无花果,ax = plt.subplots(figsize =(15,8))df.plot(x = 'Date', y = ['Deaths'], kind = 'line', ax = ax)df.plot(x = 'Date', y= ['Cases'], kind = 'bar', ax = ax, colormap="RdYlBu")

更新:要将日期用作索引

将pandas导入为pd导入matplotlib.pyplot作为pltdf = pd.read_csv(a.csv")df.set_index("Date",inplace = True)无花果,ax = plt.subplots(figsize =(15,8))df.plot( y = ['死亡'], kind = 'line', ax = ax)df.plot(y = ['Cases'],kind ='bar',ax = ax,colormap ="RdYlBu")

Date 为索引的数据框.绘制此图将得到与上述相同的图.

 死亡案例日期2020-03-01 5 142020-03-02 3 132020-03-03 6 222020-03-04 9 242020-03-05 5 262020-03-06 2 412020-03-07 10 282020-03-08 2 632020-03-09 10 1222020-03-10 15 1552020-03-11 202002020-03-12 152692020-03-13 20 360

I am trying to plot a line over a bar chart, but when I plotted the line the bar chart disappeared - and so did my x index values. Can anyone help me plot the line and the bar in the same figure? Thank you. My problem, I believe, is that the line and bar charts are overuling each other - but I want them to coexist.

plt.figure()
totalconfirm['Cases'].plot(kind='bar', rot=15, title="Cases per Day in all Michigan Counties", color='r', label = 'Confirmed Cases')
totalprob['Cases'].plot(kind = 'bar', rot=15, bottom=totalconfirm['Cases'], color = 'b', label = 'Probable Cases')
totalconfirm['Deaths'].plot(color = 'black', label = 'Deaths')

ax = plt.gca()
ax.xaxis.set_major_locator(plt.MaxNLocator(10)) 

ax.text(0.05, 0.95, "Total Tests: " + str(totaltotaltest), fontsize=10, transform=ax.transAxes,
        verticalalignment='top', bbox = dict(boxstyle='round', facecolor='wheat', alpha=0.5))

ax.set_xticklabels(df.index.format(), rotation='vertical', size=5)
plt.locator_params(axis='x', nbins=30)

plt.legend(loc="upper right")
plt.show()

Additionally, how would I move this text box to the right side under the legend?

Have looked at matplotlib plot bar and line charts together, Pandas plot bar chart over line it didn't cover what I wanted I think.

Datasets:

Date
2020-03-01     0
2020-03-02     0
2020-03-03     0
2020-03-04     0
2020-03-05     0
2020-03-06     0
2020-03-07     0
2020-03-08     0
2020-03-09     0
2020-03-10     0
2020-03-11     0
2020-03-12     0
2020-03-13     0
2020-03-14     0
2020-03-15     0
2020-03-16     1
2020-03-17     0
2020-03-18     3
2020-03-19     5
2020-03-20     2
2020-03-21     8
2020-03-22    13
2020-03-23    21
2020-03-24    25
2020-03-25    24
2020-03-26    46
2020-03-27    49
2020-03-28    66
2020-03-29    62
2020-03-30    80
              ..
2020-06-19    11
2020-06-20     8
2020-06-21     8
2020-06-22    14
2020-06-23     9
2020-06-24     6
2020-06-25     8
2020-06-26     6
2020-06-27    11
2020-06-28     7
2020-06-29     7
2020-06-30     6
2020-07-01     7
2020-07-02    10
2020-07-03     9
2020-07-04     6
2020-07-05     5
2020-07-06     7
2020-07-07    10
2020-07-08     7
2020-07-09     8
2020-07-10     5
2020-07-11     7
2020-07-12     6
2020-07-13     4
2020-07-14     0
2020-07-15     6
2020-07-16     2
2020-07-17     3
2020-07-18     0
Name: Deaths, dtype: int64

Date
2020-03-01      14
2020-03-02      13
2020-03-03      22
2020-03-04      24
2020-03-05      26
2020-03-06      41
2020-03-07      48
2020-03-08      63
2020-03-09     122
2020-03-10     155
2020-03-11     200
2020-03-12     269
2020-03-13     360
2020-03-14     364
2020-03-15     462
2020-03-16     702
2020-03-17     766
2020-03-18     861
2020-03-19     738
2020-03-20     933
2020-03-21     784
2020-03-22     793
2020-03-23    1208
2020-03-24    1075
2020-03-25    1160
2020-03-26    1128
2020-03-27    1289
2020-03-28    1041
2020-03-29     893
2020-03-30    1532
              ... 
2020-06-19     285
2020-06-20     189
2020-06-21     227
2020-06-22     398
2020-06-23     441
2020-06-24     419
2020-06-25     386
2020-06-26     368
2020-06-27     296
2020-06-28     304
2020-06-29     482
2020-06-30     481
2020-07-01     539
2020-07-02     470
2020-07-03     397
2020-07-04     273
2020-07-05     374
2020-07-06     605
2020-07-07     554
2020-07-08     607
2020-07-09     509
2020-07-10     478
2020-07-11     276
2020-07-12     280
2020-07-13     513
2020-07-14     382
2020-07-15     312
2020-07-16     228
2020-07-17     141
2020-07-18       8
Name: Cases, dtype: int64

解决方案

I have done this using plotly. I have modified the Deaths to increase the value so that the line graph is seen.

DataFrame:

 Date  Deaths  Cases
0   2020-03-01       5     14
1   2020-03-02       3     13
2   2020-03-03       6     22
3   2020-03-04       9     24
4   2020-03-05       5     26
5   2020-03-06       2     41
6   2020-03-07      10     28
7   2020-03-08       2     63
8   2020-03-09      10    122
9   2020-03-10      15    155
10  2020-03-11      20    200
11  2020-03-12      15    269
12  2020-03-13      20    360
13  2020-03-14      30    364
14  2020-03-15      40    462
15  2020-03-16     150    702
16  2020-03-17      70    766
17  2020-03-18      50    861
18  2020-03-19      30    738
19  2020-03-20     100    933

from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd
import numpy as np

df = pd.read_csv("a.csv")
fig = make_subplots(1,1)

fig.add_trace(go.Bar(x=df['Date'], y=df['Cases'],
                     name='Cases',
                     marker_color = 'yellow',
                     opacity=0.4,
                     marker_line_color='rgb(8,48,107)',
                     marker_line_width=2),
              row = 1, col = 1)

fig.add_trace(go.Scatter(x=df['Date'], y=df['Deaths'], line=dict(color='red'), name='Deaths'),
              row = 1, col = 1)

fig.show()

Diagram:

Using pandas plotting

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("a.csv")

fig, ax = plt.subplots(figsize = (15,8))
df.plot(x = 'Date', y = ['Deaths'], kind = 'line', ax = ax)
df.plot(x = 'Date', y= ['Cases'], kind = 'bar', ax = ax, colormap="RdYlBu")

Update: To use Date as index

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("a.csv")
df.set_index("Date", inplace=True)

fig, ax = plt.subplots(figsize = (15,8))
df.plot( y = ['Deaths'], kind = 'line', ax = ax)
df.plot( y= ['Cases'], kind = 'bar', ax = ax, colormap="RdYlBu")

Dataframe with Date as index. Plotting this will give the same plot as above.

            Deaths  Cases
Date
2020-03-01       5     14
2020-03-02       3     13
2020-03-03       6     22
2020-03-04       9     24
2020-03-05       5     26
2020-03-06       2     41
2020-03-07      10     28
2020-03-08       2     63
2020-03-09      10    122
2020-03-10      15    155
2020-03-11      20    200
2020-03-12      15    269
2020-03-13      20    360

这篇关于如何在条形图上绘制一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆