Home > database >  Is it possible to artificially expand the chart axis in a color chart?
Is it possible to artificially expand the chart axis in a color chart?

Time:09-21

I have created pcolor plot, based on df table:

import numpy as np
import pandas as pd

rng = np.random.default_rng()
df = pd.DataFrame(rng.integers(1, 32, size=(100, 3)), columns=list('xyw'))

sample_pvt = df.pivot_table(index='x',columns='y')  
sample_pvt.columns = sample_pvt.columns.droplevel(0)

fig, axs = plt.subplots(figsize=(10,10))
orig_map=plt.cm.get_cmap('viridis')
reversed_map = orig_map.reversed()

plt.pcolor(sample_pvt, cmap = reversed_map)
plt.xticks(np.arange(len(sample_pvt.columns)) 0.5, sample_pvt.columns)
plt.yticks(np.arange(len(sample_pvt.index)) 0.5, sample_pvt.index)

cbar = plt.colorbar()
plt.show()

Result:

enter image description here

Is it possible to artificially expand the chart on the x-axis to always 40 fields, and on the y-axis to 60 fields and leave these fields blank?

Note: Sometimes I have data with incomplete axis ranges as the picture below:

enter image description here

Is it possible to transform the data/plot in such a way that the x axis always shows values ​​from 1 to 40, and the y axis shows values ​​from 1 to 60?

CodePudding user response:

You can .reindex before plotting so that the DataFrame you plot always has the rows and columns you want.

sample_pvt = sample_pvt.reindex(range(1, 61)).reindex(range(1, 41), axis=1)

# Then your plotting code
fig, axs = plt.subplots(figsize=(10, 15))
orig_map=plt.cm.get_cmap('viridis')
reversed_map = orig_map.reversed()

plt.pcolor(sample_pvt, cmap = reversed_map)
plt.xticks(np.arange(len(sample_pvt.columns)) 0.5, sample_pvt.columns)
plt.yticks(np.arange(len(sample_pvt.index)) 0.5, sample_pvt.index)

cbar = plt.colorbar()
plt.show()

enter image description here

  • Related