简体   繁体   中英

Dataframe contourf plot Python

I have a dataframe with a lot of columns. I try to plot a contourf, but I have error like "Failed to convert value(s) to axis units".

x = np.arange(len(df.index))
y = np.arange(len(df.columns))
X,Y = np.meshgrid(x,y)

cs = ax.contourf(X,Y, df.values, cmap=cm.coolwarm)
cbar = plt.colorbar(cs)
plr.show()

What am I doing wrong?

The columns and rows are mixed up. You either need to use x for the columns and y for the index direction, or to transpose the values ( ax.contourf(X,Y, df.values.T, cmap='coolwarm') ).

Here is an example with toy data, and x and y interchanged:

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(10, 25))
x = np.arange(len(df.columns))
y = np.arange(len(df.index))
X, Y = np.meshgrid(x, y)

fig, ax = plt.subplots()
cs = ax.contourf(X, Y, df.values, cmap='coolwarm')
plt.colorbar(cs)
plt.show()

示例等高线图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM