簡體   English   中英

如何總結 x 軸上的日期和年份?

[英]How summarize days and years on x axis?

我需要“添加更多細節”,然后我才寫了這封信,抱歉。 伙計們,我需要幫助!

我有兩個df。 首先 - 生產/銷售。

# produced_sell_df
sell_gold = pd.DataFrame({'y': ['2018-01-01', '2019-01-01', '2020-01-01'], 
'produced': [150000, 300000, 500000],
'sell': [93367, 210172, 424876]})

sell_gold['y'] = pd.to_datetime(sell_gold['y'],format = '%Y-%m-%d')
sell_gold['y'] = sell_gold['y'].dt.year
sell_gold = sell_gold.set_index('y')

第二 - 3 年的黃金價值(本例中隨機設置)

# gold
testdate = pd.date_range(start=pd.datetime(2018, 1, 1), end=pd.datetime(2020, 10, 23))
testdate = pd.DataFrame(testdate)
testdate['price'] = np.random.randint(300, 500, testdate.shape[0])
testdate = testdate.rename(columns={0: "date"})
testdate = testdate.set_index('date')

我想在一張圖表中總結一下這些 df:

#  metall + sell

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# data

data = [
# scatter-line
    go.Scatter(x=testdate.index, 
               y=testdate.price, 
               name="price",
               line=dict(color='#f5c541')
              ),
    
    go.Bar(x=df.index, # assign x as the dataframe column 'x'
           y=sell_gold['produced'],
           name = 'produced'
          ),
    
    go.Bar(x=df.index, # assign x as the dataframe column 'x'
           y=sell_gold['sell'],
           name = 'sell'
          )
]

for i in data:
    fig.append_trace(i)


# Add figure title
fig.update_layout(
    barmode='group',
    plot_bgcolor='rgba(0,0,0,0)',
    title_text="Серебро (2018 - 2020 год)",
    title_x = 0.47,
    width=1024,
    height=600,
    legend=dict(x=.45, 
                xanchor="center", 
                orientation="h")
)

# Set x-axis title
fig.update_xaxes(title_text="<b>Годы</b>",
                 gridcolor='lightblue')

# Set y-axes titles
fig.update_yaxes(title_text="<b>Цена на серебро</b> (р./грамм)", 
                 secondary_y=False,
#                  gridcolor='#f5c541'
                )
fig.update_yaxes(title_text="<b>Шт.</b>", 
                 secondary_y=True,
                 gridcolor='#1167b1'
                )
fig.update_yaxes(title_text="<b>Шт.</b>", 
                 secondary_y=True,
                 gridcolor='#1167b1'
                )

fig.show()

我需要一張 plot 兩張圖表 - 黃金價格(散點圖)和 2 根柱 - 每年的鑄造量/賣出量。 我有錯誤。 你能幫助我嗎?

fig = make_subplots(specs=[[{"secondary_y": True}]])


trace0 = go.Scatter(x=df.index, 
                    y=df.gold,
                    name="Золото",
                    line=dict(color='#f5c541'),
                    yaxis='y1')

trace1 = go.Bar(x=sell_silver.index, # assign x as the dataframe column 'x'
                y=sell_silver['Реализовано'],
                name = 'Реализовано', 
                marker_color='green',
                opacity=0.1,
                marker_line_color = 'green',
                marker_line_width = 0.5,
                yaxis='y2',
                width=100
               )

trace2 = go.Bar(x=sell_silver.index, # assign x as the dataframe column 'x'
                y=sell_silver['Тираж'],
                name = 'Тираж',
                marker_color='red',
                opacity=0.1,
                marker_line_color = 'red',
                marker_line_width = 0.5,
                yaxis='y2',
                width=100,
          )
fig.add_trace(trace0,
              secondary_y=True)
fig.add_trace(trace1,
              secondary_y=False)
fig.add_trace(trace2,
              secondary_y=False)

fig.update_layout(
#     barmode='stack',    
    barmode='relative',
    plot_bgcolor='rgba(0,0,0,0)',
    title_text="Серебро (2018 - 2020 год)",
    title_x = 0.5,
    width=1024,
    height=600,
    legend=dict(x=.45, 
                xanchor="center", 
                orientation="h"))

# Set x-axis title
fig.update_xaxes(title_text="<b>Годы</b>",
                 gridcolor='lightblue')

# Set y-axes titles

fig.update_yaxes(title_text="<b>Цена на серебро</b> (р./грамм)", 
                 secondary_y=True,
#                  gridcolor='#f5c541'
                side='left')

fig.update_yaxes(title_text="<b>Шт.</b>", 
                 secondary_y=False,
                 gridcolor='#1167b1',
                 side='right')

fig.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM