簡體   English   中英

Python Matplotlib plot 單圖多數據

[英]Python Matplotlib plot multiple data in single graph

我正在嘗試找到一種有意義的方法來 plot 單個圖形中的所有數據。

經過所有轉換后,我的數據看起來像這樣 -

庫存 日期 行動 數量 交易百分比
Arihant Tournesol 有限公司 2022-12-27 處理 2836900.0 28.660477
阿西特 C Mehta Financial Services Ltd. 2022-12-28 處理 380000.0 7.839254
HCKK風險投資有限公司 2022-12-28 獲得 1866917.0 50.321213
HCKK風險投資有限公司 2022-12-28 處理 1866917.0 50.321213
馬法特拉爾工業有限公司 2022-12-27 獲得 11500000.0 16.316917
馬法特拉爾工業有限公司 2022-12-27 處理 11500000.0 16.316917
米爾格雷金融投資有限公司 2022-12-28 撤銷 126000.0 6.331658
Shreeshay 工程師有限公司 2022-12-26 處理 2460000.0 18.631565
須麻屋株式會社 2022-12-27 保證 10108008.0 40.885038
紗辛迪加有限公司 2022-12-28 處理 330000.0 8.800000
normal plotting will be like 
df.plot(kind='bar', x='Stock', y='Quantity', color='olivedrab')

但這將給出 2 數據 plot,我試圖將所有數據繪制在單個圖表上

另一種方法可以做到 -

ax = df.plot(kind='bar', x='Stock', y='Quantity', figsize=(16, 8), color='olivedrab')

ax1 = df['Traded %'].plot(secondary_y=True, ax=ax, color='indianred')

ax.tick_params(axis='x', labelsize=14, rotation=90)
plt.show()

這會生成這樣的圖片,請檢查鏈接 - 但我錯過了日期。 [1]: https://i.stack.imgur.com/9VNYC.jpg

拜托,如果您有任何其他方式將它們放在一張圖中的 plot,請分享...

要 plot 同一張圖上的多個數據集,您可以嘗試如下操作:

# Import the Matplotlib library
import matplotlib.pyplot as plt

# Prepare the data for the first dataset
x1 = [1, 2, 3, 4]  # x-values
y1 = [10, 20, 25, 30]  # y-values

# Prepare the data for the second dataset
x2 = [1, 2, 3, 4]  # x-values
y2 = [5, 10, 15, 20]  # y-values

# Create a figure and an axis
fig, ax = plt.subplots()

# Plot the first dataset on the axis
ax.plot(x1, y1,  # x and y values
        color='r',  # line color
        linestyle='--',  # line style
        label='dataset 1')  # label for the legend

# Plot the second dataset on the axis
ax.plot(x2, y2,  # x and y values
        color='b',  # line color
        linestyle='-',  # line style
        label='dataset 2')  # label for the legend

# Add a legend to the plot
ax.legend()

# Add labels to the x and y axes
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')

# Add a title to the plot
ax.set_title('Multiple datasets')

# Show the plot
plt.show()

這將創建一個包含兩條線的圖表,一條用於每個數據集,具有不同的 colors 和線 styles。

圖例將 label 每行與相應的數據集。

試試這個:

import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns

ax1 = sns.set_style(style=None, rc=None )

fig, ax1 = plt.subplots(figsize=(12,6))

sns.lineplot(data = df,x='Stock', y='Traded', marker='o', sort = False, ax=ax1)
ax2 = ax1.twinx()

sns.barplot(data = df, x='Stock', y='Quantity', alpha=0.5, ax=ax2)

暫無
暫無

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

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