Skip to content Skip to sidebar Skip to footer

Matplotlib Plot Changes With Different Data Structure (same Data)

Creating a scatter plot with matplotlib is giving me an empty or correct plot depending on whether I construct a pandas dataframe from a dictionary with from_dict or from a list wi

Solution 1:

This is happening because creating a dataframe from a dictionary (using the above method) it is not inferring datatypes. It may in later versions but I'm running 0.20.1. For demonstration purposes I renamed your dfs:

df_fromdict       = pd.DataFrame.from_dict(dict_data, orient='index').T
df_fromdict.slave = pd.to_datetime(df_fromdict.slave)

df_fromdict.dtypes
master              object
slave       datetime64[ns]
baseline            object # notice here your baseline column isnot recognized as int
coh                 object
coh_mean            object
coh_std             objectdtype:object


df_fromlist = pd.DataFrame([list_data], columns=columns)
df_fromlist.slave = pd.to_datetime(df_fromlist.slave)

df_fromlist.dtypes
master              object
slave       datetime64[ns]
baseline             int64 # recognized as int bydefault
coh                 object
coh_mean           float64
coh_std            float64
dtype:object

Try the following as a test and your first scatterplot should appear as expected:

fig, axes  = plt.subplots()
axes.scatter(df_fromdict.slave.values, df_fromdict.baseline.astype(int).values)
plt.show()

Post a Comment for "Matplotlib Plot Changes With Different Data Structure (same Data)"