csv df.plot -仅允许散点图

oprakyz7  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(97)

我有两个CSV文件,我加载到Pandas数据框。然后我绘制一些列到一个图形。不知何故,一个图形可以是任何类型(线,面积,散点,酒吧等),但其他只能是散点。为什么?
下面是我的工作代码,当我将最后一行的kind=“scatter”改为kind=“line”(或其他类型)时,它不会绘图:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('current_speed.csv', sep=';',
                 parse_dates={ 'datetime':['year','month','day','time_utc'] })

df1 = pd.read_csv('wind_speed.csv', sep=';',
                 parse_dates={ 'datetimex':['year','month','day','time_utc'] })

fig, ax1 = plt.subplots(1,1)
ax2 = ax1.twinx()

df.plot(x = 'datetime', y = 'curr_speed', color="blue",legend=False, ax= ax1,linewidth=1,alpha=0.4,kind="line")
df1.plot('datetimex', 'wind_speed',ax=ax2, kind="scatter")

以下是数据文件:风速.csv

year;month;day;time_utc;wind_dir;wind_speed;wind_gust
2022;10;27;00:00;280;4.3;8.5
2022;10;27;01:00;268;6.4;8.3
2022;10;27;02:00;272;6.5;8.9

电流_速度.csv

year;month;day;hour;min;sec;time_utc;curr_speed;curr_dir
2022;10;27;13;36;42;13:36;0.174;21.75
2022;10;27;14;36;42;14:36;0.195;38.42
2022;10;27;15;36;42;15:36;0.146;34.15
5gfr0r5j

5gfr0r5j1#

这允许您选择所需的任何类型:

import pandas as pd
import matplotlib.pyplot as plt

wind_speed_df = pd.DataFrame({
    'year': [2022, 2022, 2022],
    'month': [10, 10, 10],
    'day': [27, 27, 27],
    'time_utc': ['00:00', '01:00', '02:00'],
    'wind_dir': [280, 268, 272],
    'wind_speed': [4.3, 6.4, 6.5],
    'wind_gust': [8.5, 8.3, 8.9]
})

current_speed_df = pd.DataFrame({
    'year': [2022, 2022, 2022],
    'month': [10, 10, 10],
    'day': [27, 27, 27],
    'hour': [13, 14, 15],
    'min': [36, 36, 36],
    'sec': [42, 42, 42],
    'time_utc': ['13:36', '14:36', '15:36'],
    'curr_speed': [0.174, 0.195, 0.146],
    'curr_dir': [21.75, 38.42, 34.15]
})

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()

wind_speed_df.plot(x='time_utc', y='wind_speed', ax=ax2, kind='line', color='red')

current_speed_df.plot(x='time_utc', y='curr_speed', ax=ax1, kind='line', color='blue')

ax1.set_xlabel('Time (UTC)')
ax1.set_ylabel('Current Speed (m/s)', color='blue')
ax2.set_ylabel('Wind Speed (m/s)', color='red')
plt.title('Current and Wind Speeds vs Time (UTC)')

plt.show()

import pandas as pd
import matplotlib.pyplot as plt

wind_speed_df = pd.DataFrame({
    'year': [2022, 2022, 2022],
    'month': [10, 10, 10],
    'day': [27, 27, 27],
    'time_utc': ['00:00', '01:00', '02:00'],
    'wind_dir': [280, 268, 272],
    'wind_speed': [4.3, 6.4, 6.5],
    'wind_gust': [8.5, 8.3, 8.9]
})

current_speed_df = pd.DataFrame({
    'year': [2022, 2022, 2022],
    'month': [10, 10, 10],
    'day': [27, 27, 27],
    'hour': [13, 14, 15],
    'min': [36, 36, 36],
    'sec': [42, 42, 42],
    'time_utc': ['13:36', '14:36', '15:36'],
    'curr_speed': [0.174, 0.195, 0.146],
    'curr_dir': [21.75, 38.42, 34.15]
})

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()

wind_speed_df.plot(x='time_utc', y='wind_speed', ax=ax2, kind='scatter', color='red')

current_speed_df.plot(x='time_utc', y='curr_speed', ax=ax1, kind='line', color='blue')

ax1.set_xlabel('Time (UTC)')
ax1.set_ylabel('Current Speed (m/s)', color='blue')
ax2.set_ylabel('Wind Speed (m/s)', color='red')
plt.title('Current and Wind Speeds vs Time (UTC)')

plt.show()

df1只允许散点图的原因是您正在为此图使用ax2,它与某些类型的图不兼容。次y轴用于与主y轴(ax1)具有不同比例的图,如散点图。

相关问题