import pandas as pd import seaborn as sns df78 = pd.readexcel('workwithprotein.xlsx',…
- User
import pandas as pd import seaborn as sns
df_78 = pd.read_excel('work_with_protein.xlsx', dtype=str, index_col=None)
df_78['value'] = df_78['value'].astype(int)
ax = sns.barplot(data=df_78, x="point", y="value", hue="protein", errorbar=None)
sns.stripplot(data=df_78, x="point", y="value", hue="protein", dodge=True, size=4, ax=ax, color='black', legend=False)
Установка логарифмической шкалы по оси y
ax.set(yscale='log') ax.set_ylim(100, 10000000000) Как легенду переместить из левого нижнего угла в верхний
- Assistant
You can move the legend from the lower left corner to the upper part of the plot by using the
.legend()method.Here's how you can do it:
- First, assign the
ax.legend()function call to a variable, which will store the legend object:
legend = ax.legend()- Next, you can use the
.set_position()method to set the position of the legend to the desired location. The parameters forset_position()are the coordinates of the legend's upper-left corner relative to the axes' coordinates. For example, to move the legend to the upper right corner, you can set the position as(0.7, 0.8):
- First, assign the
- User
AttributeError: 'Legend' object has no attribute 'set_position'
- Assistant
Apologies for the confusion. It seems that the
set_position()method is not available for theLegendobject in seaborn. Instead, you can use thebbox_to_anchorparameter of thelegend()function to specify the position of the legend.Here's the updated code for moving the legend to the upper right corner:
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt df_78 = pd.read_excel('work_with_protein.xlsx', dtype=str, index_col=None) df_78['value'] = df_78['value'].astype(int) ax = sns.barplot(data=df_78, x="point", y="value", hue="protein", errorbar=None) sns.stripplot(data=df_78, x="point", y="value", hue="protein", dodge=True, size=4, ax=ax, color='black', legend=False) # Set log scale on y-axis ax.set(yscale='log') ax.set_ylim(100, 10000000000) # Move the legend to the upper right corner legend = ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left') plt.show() - User
он переместился в правый верхний угол. Причем за график
- Assistant
Apologies for the confusion. If you want to move the legend to the upper right corner within the plot area, you can modify the
bbox_to_anchorparameter to specify a position relative to the axes' coordinates.Here's the updated code to move the legend to the upper right corner within the plot area:
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt df_78 = pd.read_excel('work_with_protein.xlsx', dtype=str, index_col=None) df_78['value'] = df_78['value'].astype(int) ax = sns.barplot(data=df_78, x="point", y="value", hue="protein", errorbar=None) sns.stripplot(data=df_78, x="point", y="value", hue="protein", dodge=True, size=4, ax=ax, color='black', legend=False) # Set log scale on the y-axis ax.set(yscale='log') ax.set_ylim(100, 10000000000) # Move the legend to the upper right corner within the plot area legend = ax.legend(loc='upper right') plt.show()In this updated code,
loc='upper right'specifies the location of the legend within the plot area, and the parameter is not necessary. - User
а можно легенду разбить на 2 колонки?
- Assistant
Yes, you can split the legend into two columns by using the
ncolparameter of thelegend()function. Thencolparameter specifies the number of columns in the legend.Here's the updated code to split the legend into two columns:
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt df_78 = pd.read_excel('work_with_protein.xlsx', dtype=str, index_col=None) df_78['value'] = df_78['value'].astype(int) ax = sns.barplot(data=df_78, x="point", y="value", hue="protein", errorbar=None) sns.stripplot(data=df_78, x="point", y="value", hue="protein", dodge=True, size=4, ax=ax, color='black', legend=False) # Set log scale on the y-axis ax.set(yscale='log') ax.set_ylim(100, 10000000000) # Move the legend to the upper right corner within the plot area and split into two columns legend = ax.legend(loc='upper right', ncol=2) plt.show()In this updated code,
ncol=2specifies that the legend should be split into two columns. Feel free to adjust the value to the desired number of columns in the legend. - User
а как увеличить ширину столбиков на графике?
- Assistant
To increase the width of the bars in the barplot, you can use the
widthparameter of thebarplot()function in seaborn. Thewidthparameter controls the width of the bars on the plot.Here's an example of how you can increase the width of the bars:
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt df_78 = pd.read_excel('work_with_protein.xlsx', dtype=str, index_col=None) df_78['value'] = df_78['value'].astype(int) # Increase the width of the bars by setting the width parameter ax = sns.barplot(data=df_78, x="point", y="value", hue="protein", width=0.8, # Adjust the width value as desired errorbar=None) sns.stripplot(data=df_78, x="point", y="value", hue="protein", dodge=True, size=4, ax=ax, color='black', legend=False) # Set log scale on the y-axis ax.set(yscale='log') ax.set_ylim(100, 10000000000) # Move the legend to the upper right corner within the plot area and split into two columns legend = ax.legend(loc='upper right', ncol=2) plt.show() - User
А как убрать границы рисунка?
- Assistant
To remove the borders around the plot, you can use the
spinesattribute of theAxesobject in matplotlib.Here's an example of how you can remove the borders:
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt df_78 = pd.read_excel('work_with_protein.xlsx', dtype=str, index_col=None) df_78['value'] = df_78['value'].astype(int) ax = sns.barplot(data=df_78, x="point", y="value", hue="protein", width=0.8, errorbar=None) sns.stripplot(data=df_78, x="point", y="value", hue="protein", dodge=True, size=4, ax=ax, color='black', legend=False) ax.set(yscale='log') ax.set_ylim(100, 10000000000) # Remove the borders around the plot ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) legend = ax.legend(loc='upper right', ncol=2) plt.show() - User
а как сделать черные границы столбиков
- Assistant
To add black borders to the bars on the barplot, you can use the
edgecolorparameter of thebarplot()function in seaborn. Theedgecolorparameter controls the color of the borders of the bars.Here's an example of how you can add black borders to the bars:
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt df_78 = pd.read_excel('work_with_protein.xlsx', dtype=str, index_col=None) df_78['value'] = df_78['value'].astype(int) ax = sns.barplot(data=df_78, x="point", y="value", hue="protein", edgecolor='black', # Add black borders to the bars width=0.8, errorbar=None) sns.stripplot(data=df_78, x="point", y="value", hue="protein", dodge=True, size=4, ax=ax, color='black', legend=False) ax.set(yscale='log') ax.set_ylim(100, 10000000000) legend = ax.legend(loc='upper right', ncol=2) plt.show()In this example, I've added the
edgecolor='black'parameter to set the border color of the bars to black. You can adjust the color to any other valid color value if desired.