Currently, I am trying to output image outputs for my data.
My original source is like this:
total_dict = {"A" : {"A1" : [1, 2, 3, 4, 5], "A2" : [2, 3, 4, 5, 6]}, "B" : {"B1" : [11, 22, 13, 34, 5], "B2" : [12, 31, 42, 52, 16]},"C" : {"C1" : [12, 22, 33, 4, 5], "C2" : [42, 33, 42, 15, 6]}, "D" : {"D1" : [1, 23, 35, 4, 5], "D2" : [21, 23, 34, 5, 6]}}
Now, I am trying to create 4 heat maps for each sub-library: A, B, C and D.
My program is:
import pandas as pd
import seaborn as sns
for sub in total_dict:
df = pd.DataFrame(total_dict[sub])
image = sns.heatmap(df, cmap="YlGnBu", linewidths = 0.1, vmax = 100)
print (image)
However, it does not print out 4 separate images as I expected. The final result is:
heatmap_result
Could you please suggest me any method that I can use to get my expected 4 separate outputs as well as be able to save them in 4 separate files.
Thanks for your help.
You can define a new figure, plot the heat-map, and save the figure.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
for index, sub in enumerate(total_dict):
df = pd.DataFrame(total_dict[sub])
# create a new figure
fig = plt.figure()
image = sns.heatmap(df, cmap="YlGnBu", linewidths = 0.1, vmax = 100)
fig_name = 'test_%s.png' %(index)
fig.savefig(fig_name)
Related
This question already has answers here:
Annotated heatmap with multiple color schemes
(2 answers)
How to create a heat-map with multiple colormaps?
(2 answers)
Heatmap with multiple colormaps by column
(2 answers)
Closed last month.
I am trying to build a heatmap with seaborn that each row of the 'table' get a different color range. Example:
A: yellow
B: blue
C: green
D: red
must be in the same table for comparing the own row along the year and different rows in the same month. Is my question clear, thanks so much?
I only could plot the heatmap comparing all the table:
d = {'jan': [44, 2, 3, 4],
'feb': [33, 4, 6, 8],
'mar': [50, 10, 15, 20],
'abr': [11, 12, 13, 14],
'mai': [3, 40, 6, 60],
'jun': [40, 8, 12, 16]}
idx = ['A', 'B', 'C', 'D']
df = pd.DataFrame(d, index = idx)
sns.heatmap(df, annot = True, cmap = 'Blues')
plt.yticks(rotation = 0)
That is what i trying to plot. Made with google sheets:
I suggest you to do one plot per line and put them into a plt subplot:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
d = {'jan': [44, 2, 3, 4], 'feb': [33, 4, 6, 8], 'mar': [50, 10, 15, 20], 'abr': [11, 12, 13, 14], 'mai': [3, 40, 6, 60], 'jun': [40, 8, 12, 16]}
idx = ['A', 'B', 'C', 'D']
df = pd.DataFrame(d, index = idx)
cm = ['Blues', 'Reds', 'Greens', 'Purples']
f, axs = plt.subplots(4, 1, gridspec_kw={'hspace': 0})
counter = 0
for index, row in df.iterrows():
sns.heatmap(np.array([row.values]), yticklabels=[idx[counter]], xticklabels=df.columns, annot=True, fmt='.2f', ax=axs[counter], cmap=cm[counter], cbar=False)
counter += 1
plt.show()
Output :
I am trying to display a grouped bar chart by categorical values on columns.
An example data is below.
df = pd.DataFrame({'Type': ['A','A','A','A','A','A','A','A','A','B','B','B','B','B','B','B','B','B'],
'Ratio': [3, 3, 3, 5, 5, 5, 7, 7, 7,3, 3, 3, 5, 5, 5, 7, 7, 7],
'Method': ['X','Y','Z','X','Y','Z','X','Y','Z','X','Y','Z','X','Y','Z','X','Y','Z'],
'Result': [90, 85, 96, 89, 82, 80, 78, 72, 75, 91, 82, 94, 87, 86, 84, 71, 78, 86]})
Values in "Type" column can be on the same chart or two subplot graphs. The Y axis must display the value in "Result" and the legend of the bar chart must display the value in "Method"
Example
My dataset is quite large compared to the example above so a loop or function would be more useful.
My code below is not working as I want.
fig, ax = plt.subplots(figsize=(4,3))
for t in ["A", "B"]:
df1 = df.loc[df["Type"] == t]
for r, w in zip([3,5,7], [5, 10, 15]):
df2 = df1.loc[df1["Ratio"] == r]
for m, i in zip(["X","Y","Z"], range(3)):
df3 = df2.loc[df2["Method"] == m]
label = str(m)
ax.bar((w+i), df3["Result"], label=label)
plt.savefig("test.svg")
Consider a subset loop with pandas DataFrame.groupby, then pivoting data with DataFrame.pivot_table, and finally running plot with DataFrame.plot:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({
'Type': ['A','A','A','A','A','A','A','A','A','B','B','B','B','B','B','B','B','B'],
'Ratio': [3, 3, 3, 5, 5, 5, 7, 7, 7,3, 3, 3, 5, 5, 5, 7, 7, 7],
'Method': ['X','Y','Z','X','Y','Z','X','Y','Z','X','Y','Z','X','Y','Z','X','Y','Z'],
'Result': [90, 85, 96, 89, 82, 80, 78, 72, 75, 91, 82, 94, 87, 86, 84, 71, 78, 86]
})
num_types = len(df["Type"].unique())
fig, axes = plt.subplots(figsize=(12,4), ncols=num_types, nrows=1)
# FLATTEN MULTI-DIMENSIONAL ARRAY
axes = np.ravel(axes)
# ITERATE THROUGH TYPE GROUPS
for (g, df), ax in zip(df.groupby("Type"), axes):
# PIVOT FOR WIDE DATA FOR BAR CHART
(
df.pivot_table(index="Ratio", columns="Method", values="Result", aggfunc="sum")
.plot(kind="bar", ax=ax, rot=0, title=f"{g} - Bar Chart")
)
plt.show()
fig.savefig("bar_chart.png")
plt.clf()
plt.close()
import pandas as pd
import matplotlib.pyplot as plt
grouped_A = df[df['Type'] == 'A']
grouped_B = df[df['Type'] == 'B']
fig, ax = plt.subplots(1, 2, figsize=(15, 5))
for r, w in zip([3,5,7], [5, 10, 15]):
A = grouped_df_A.loc[grouped_df_A["Ratio"] == r]
B = grouped_df_B.loc[grouped_df_B["Ratio"] == r]
for m, i in zip(["X","Y","Z"], range(3)):
methodA = A.loc[A["Method"] == m]
methodB = B.loc[B["Method"] == m]
label = str(m)
ax[0].bar((w+i), methodA["Result"], label=label)
ax[0].set_title("Type A")
ax[1].bar((w+i), methodB["Result"], label=label)
ax[1].set_title("Type B")
plt.show()
This should work for multiple types in your Type column creating vertical subplots for each.
item_list = list(df.Type.unique())
fig, ax = plt.subplots(len(item_list), figsize=(12, 8))
for t in range(len(item_list)):
df1 = df.loc[df["Type"] == item_list[t]]
for r, w in zip([3,5,7], [5, 10, 15]):
df2 = df1.loc[df1["Ratio"] == r]
for m, i in zip(["X","Y","Z"], range(3)):
df3 = df2.loc[df2["Method"] == m]
label = str(m)
ax[t].bar((w+i), df3["Result"], label=label)
ax[t].set_title(f'Type{item_list[t]}')
Plots:
I have a list that has counts of some values and I want to make a bar plot to see the counts as bars. The list is like:
print(lii)
# Output
[46, 11, 9, 20, 3, 15, 8, 63, 11, 9, 24, 3, 5, 45, 51, 2, 23, 9, 17, 1, 1, 37, 29, 6, 3, 9, 25, 5, 43]
I want something like this plot with each list value as a bar and its value on top:
I tried the following code but it plotted the list as a series:
plt.figure(figsize=(30, 10))
plt.plot(lii)
plt.show()
Any help is appreciated.
I believe you want something like this:
ax = sns.barplot(x=np.arange(len(lii)), y=lii)
ax.bar_label(ax.containers[0])
plt.axis('off')
plt.show()
You can do it using matplotlib pyplot bar.
This example considers that lii is the list of values to be counted.
If you already have the list of unique values and associated counts, you do not have to compute lii_unique and counts.
import matplotlib.pyplot as plt
lii = [46, 11, 9, 20, 3, 15, 8, 63, 11, 9, 24, 3, 5, 45, 51, 2, 23, 9, 17, 1, 1, 37, 29, 6, 3, 9, 25, 5, 43]
# This is a list of unique values appearing in the input list
lii_unique = list(set(lii))
# This is the corresponding count for each value
counts = [lii.count(value) for value in lii_unique]
barcontainer = plt.bar(range(len(lii_unique)),counts)
# Some labels and formatting to look more like the example
plt.bar_label(barcontainer,lii_unique, label_type='edge')
plt.axis('off')
plt.show()
Here is the output. The label above each bar is the value itself, while the length of the bar is the count for this value. For example, value 9 has the highest bar because it appears 4 times in the list.
I want to create a bar graph for a dataframe contains multiple categories, with a different color for each category. Below is my simplified code and resulting graph. The top subplot is a regular bar graph in one color, the bottom subplot is color coded but the bar width is messed up. Any suggestions? Thanks!
import random
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'Cat': [1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4],
'A': [2, 3, 6, 7, 9, 10, 15, 18, 22, 23, 24, 25],
'B': random.sample(range(1, 20), 12)})
fig = plt.figure(figsize=(15, 15/2.3))
ax = plt.subplot(2, 1, 1)
plt.bar(df.A, df.B)
plt.xlim(0, 30)
ax = plt.subplot(2, 1, 2)
for cat in df.Cat.unique():
df_ = df.loc[(df.Cat==cat), :]
plt.bar(df_.A, df_.B, width=0.5)
plt.xlim(0, 30)
plt.show()
Let's say if I have Height = [3, 12, 5, 18, 45] and plot my graph then the yaxis will have ticks starting 0 up to 45 with an interval of 5, which means 0, 5, 10, 15, 20 and so on up to 45. Is there a way to define the interval gap (or the step). For example I want the yaxis to be 0, 15, 30, 45 for the same data set.
import matplotlib.pyplot as plt
import numpy as np
plt.plot([3, 12, 5, 18, 45])
plt.yticks(np.arange(0,45+1,15))
plt.show()
This should work
matplotlib.pyplot.yticks(np.arange(start, stop+1, step))